It seems that the question is not to show many exceptions, but to show the entire trace of the error stack. When one error occurs, execution will not receive or not cause another error. In some languages, you can naturally set the parent exception to the current exception, but this does not apply to javascript, so far I can say and look at the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference / Global_Objects / Error and https://nodejs.org/api/errors.html#errors_error_propagation_and_interception . You will need to create your own error class, which is not so difficult.
If the problem is to display a trace
Javascript stack trace is a line! Which is good if you just want to put it in some kind of journal, but bad if you want to make a more meaningful reading structure, like json.
If what you want to do does show a stack trace, you may need to convert the stack trace of the Error object to an array using something like this: https://github.com/stacktracejs/error-stack-parser , and then put this array inside your error object.
After that, you can simply save this object in your database. You will still see only one error, but you will have all the โlocationโ, โlineโ, โpathโ to it, it sounds to me what you are looking for.
If the problem is to show the parent error message and trace
If you want to keep the parent error of a trace, you probably need to create your own error class.
function MyError(message, parentError ) { this.message = message; this.stack = Error().stack; this.parentError = parentError; } MyError.prototype = Object.create(Error.prototype); MyError.prototype.name = "MyError"; function a() { b(); } function b() { try { c(); } catch ( e ) { throw new MyError( "error on b", e ); } } function c() { d(); } function d() { throw new MyError("error on d"); } function showError( e ) { var message = e.message + " " + e.stack; if ( e.parentError ) { return message + "\n" + showError( e.parentError ); } return message; } try{ a(); } catch ( e ) { console.log(showError( e )); }
If this problem displays a lot of error and trace messages
If you want to save a lot of errors in a large package, for example, for checking feedback, you can extend the error class to create an error package. I created one simple example of each of these classes.
function MyErrorPackage(message, parentError ) { this.packageErrors = []; this.message = "This package has errors. \n"; this.isValid = true; this.stack = Error().stack; this.parentError = parentError; this.addError = function addError( error ) { this.packageErrors.push( error ); this.isValid = false; this.message += "PackageError(" + this.packageErrors.length + "): " + error.stack + error.stack + "\n"; }; this.validate = function validate() { if( ! this.isValid ) { throw this; } }; } MyErrorPackage.prototype = Object.create(Error.prototype); MyErrorPackage.prototype.name = "MyErrorPackage"; function showError( e ) { var message = e.message + " " + e.stack; if ( e.parentError ) { return message + "\n" + showError( e.parentError ); } return message; } function showPackageError( e ) { var message = e.message + " " + e.stack; if ( e.parentError ) { return message + "\n" + showError( e.parentError ); } return message; } try{ var p = new MyErrorPackage(); try { throw new Error("error 1"); } catch( e1 ) { p.addError(e1); } try { throw new Error("error 2"); } catch( e2 ) { p.addError(e2); } try { throw new Error("error 3"); } catch( e3 ) { p.addError(e3); } p.validate(); } catch ( e4 ) { console.log(showError( e4 )); }
Thiago mata
source share