update webhook response (#2006)

* add hint to 'webhook not registered' error

* Convert hints to constants
This commit is contained in:
MedAliMarz
2021-07-23 20:56:18 +02:00
committed by GitHub
parent 34c1123636
commit 2a99a77b43
3 changed files with 21 additions and 8 deletions

View File

@@ -21,17 +21,21 @@ export class ResponseError extends Error {
// The HTTP status code of response
httpStatusCode?: number;
// The error code in the resonse
// The error code in the response
errorCode?: number;
// The error hint the response
hint?: string;
/**
* Creates an instance of ResponseError.
* @param {string} message The error message
* @param {number} [errorCode] The error code which can be used by frontend to identify the actual error
* @param {number} [httpStatusCode] The HTTP status code the response should have
* @param {string} [hint] The error hint to provide a context (webhook related)
* @memberof ResponseError
*/
constructor(message: string, errorCode?: number, httpStatusCode?: number) {
constructor(message: string, errorCode?: number, httpStatusCode?: number, hint?:string) {
super(message);
this.name = 'ResponseError';
@@ -41,6 +45,9 @@ export class ResponseError extends Error {
if (httpStatusCode) {
this.httpStatusCode = httpStatusCode;
}
if (hint) {
this.hint = hint;
}
}
}
@@ -91,6 +98,7 @@ export function sendErrorResponse(res: Response, error: ResponseError) {
const response = {
code: 0,
message: 'Unknown error',
hint: '',
};
if (error.name === 'NodeApiError') {
@@ -103,6 +111,9 @@ export function sendErrorResponse(res: Response, error: ResponseError) {
if (error.message) {
response.message = error.message;
}
if (error.hint) {
response.hint = error.hint;
}
if (error.stack && process.env.NODE_ENV !== 'production') {
// @ts-ignore
response.stack = error.stack;