Skip to content

Commit

Permalink
fix: fix TypeError when handling external errors
Browse files Browse the repository at this point in the history
When an error is handled which is not a FloraError, its properties are
copied to the response object. This might be a problem, e.g. when there
is a meta.headers property (which is protected in FloraErrors) and might
cause a TypeError.
  • Loading branch information
nicokaiser committed Oct 31, 2023
1 parent 1c800df commit c49b67a
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,15 @@ class Server {
_sendError(err, httpRequest, httpResponse) {
const response = new Response();

if (err.data) response.data = err.data;
if (err.meta) Object.assign(response.meta, err.meta);
response.meta.statusCode = err.httpStatusCode || new errors.FloraError().httpStatusCode;
response.error = errors.format(err, { exposeErrors: this.api.config.exposeErrors });
if (err instanceof errors.FloraError) {
if (err.data) response.data = err.data;
if (err.meta) Object.assign(response.meta, err.meta);
response.meta.statusCode = err.httpStatusCode || new errors.FloraError().httpStatusCode;
} else {
response.meta.statusCode = 500;
}

response.error = errors.format(err, { exposeErrors: this.api.config.exposeErrors });
this._sendResponse(response, httpRequest, httpResponse);
}

Expand Down

0 comments on commit c49b67a

Please sign in to comment.