I am new to frontend development - I used to be backend only for last years. I am trying to remove a error-silencer in a helper and rethrow the swallowed error with more informations. In the custom ErrorHandler I will catch the error end send the information to a backend service. I do not know how to do this in the correct way. The throwing part (simplified):
class Helper {
public doStuff(input: string): string {
try {
return mightThrowAnException(input);
} catch (err) {
(err as any).inputData = input; // this destroys the error data
throw err;
}
}
}
And catch this with
export class GlobalErrorHandler extends ErrorHandler {
constructor(
private injector: Injector,
) {
super();
}
public handleError(error: Error) {
let loggingData: any = {
errorMessage: error.message,
}
if (typeof (error as any).inputData !== "undefined") { // would like to replace this duck-typing, too
loggingData = {
...loggingData,
inputData: (error as any).inputData,
}
}
// ... more stuff here
console.log(loggingData); // will send the data to the logging server ITRL
}
}
To enrich the error I already tried to use @jsdevtools/ono package:
class HelperWithOno {
public doStuff(input: string): string {
try {
return mightThrowAnException(input);
} catch (err) {
throw ono(err, {inputData: input});
}
}
}
Which did the job. Unfortunately the TS linter does not like the usage of symbol ( https://github.com/JS-DevTools/ono/issues/13 ) so I get a red build-pipeline.
In PHP with Zend I would create a class which extends the general Exception in the namespace of the helper and add the inputData property. Then I would be able to check the type of the caught exception and handle it in the desired way.
But I am pretty new to angular and TypeScript and not sure how to perform this task.
- I could extend the
Errorclass but- would I need to copy each property from the "previous" error manually? Or should I introduce another property
previousErrorto store the original error? - where (directory) store the class-file?
- should I create an injectable factory for the errors?
- in which module would a experienced developer assume this class?
- would I need to copy each property from the "previous" error manually? Or should I introduce another property
- Wait till
onois fixed - Any other best practices?