-
Notifications
You must be signed in to change notification settings - Fork 398
ErrorHandler: invoke class specific toJSON transformation #325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
add ability to define your own JSON transformation of Errors
AFAIK, express uses And according to MDN:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify So I think that the added feature is not needed at all. If |
I think that the The protected processJsonError(error: any) {
if (!this.isDefaultErrorHandlingEnabled)
return error;
if (typeof error.toJSON === "function")
return error.toJSON();
let processedError: any = {};
if (error instanceof Error) {
const name = error.name && error.name !== "Error" ? error.name : error.constructor.name;
processedError.name = name;
if (error.message)
processedError.message = error.message;
if (error.stack && this.developmentMode)
processedError.stack = error.stack;
Object.keys(error)
.filter(key => key !== "stack" && key !== "name" && key !== "message" && (!(error instanceof HttpError) || key !== "httpCode"))
.forEach(key => processedError[key] = (error as any)[key]);
if (this.errorOverridingMap)
Object.keys(this.errorOverridingMap)
.filter(key => name === key)
.forEach(key => processedError = this.merge(processedError, this.errorOverridingMap[key]));
return Object.keys(processedError).length > 0 ? processedError : undefined;
}
return error;
} |
I think that the first version with However please write a test for your case to prove that it is working and prevent regression in the future (we will be doing huge refactor of error handling part) 😉 |
sure, I'll do it during the next days |
I think the standard error processing is covered by the existing test, so just added one for the case of an existing toJSON function. |
Just added a little sample for the documentation. Can be delete, if you don't like it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
@pleerock @NoNameProvided |
@NoNameProvided Are you sure about that? |
@19majkel94 You're right, it does not work. I've tried it, but the structure in npm is different to the repository. During your publish process the dist/ content is moved to the root. |
In that case, I suggest you to fork the repo and make the required modifications. @pleerock mentioned multiple times, he wants to move this repo and others under the @typestack organization. Then both @19majkel94 and I will be able to publish new versions. |
That's the way to go currently - just wanted to ask 🙂 |
I would rather talk with @pleerock to make the move as soon as he can but he is very busy with TypeORM right now. |
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Hi,
it would be great to have the ability to define a custom toJSON transformation of Errors before they are sent to the client.
UseCase:
We want to censor our custom Errors (extending HttpError) before they are leaving the server.
Therefore we thought to simply implement a toJSON method, like you can override the toString.
Best Regards