How to return an array of errors using graphQL

How can I return multiple error messages like this?

"errors": [ { "message": "first error", "locations": [ { "line": 2, "column": 3 } ], "path": [ "somePath" ] }, { "message": "second error", "locations": [ { "line": 8, "column": 9 } ], "path": [ "somePath" ] }, ] 

On my server, if I do throw('an error') , it returns.

 "errors": [ { "message": "an error", "locations": [ { } ], "path": ["somePath"] } ] 

I would like to return an array of all errors in the request. How to add multiple errors to the errors array?

+8
graphql hapijs
source share
2 answers

You will need to catch errors without a throw statement because you do not want to interrupt your process. Instead, you can create an array with errors and .push() errors in it. When you see fit, near the end of your process, you can check to see if there are errors inside the error array. If so, you can display them or process them as you wish.

 // example var errors = []; doSomething(function(err,res){ if(err){ errors.push(err); } console.log("we did a thing"); doSomethingElse(function(err,res2){ if(err){ errors.push(err); }; console.log("we did another thing"); // check and throw errors if(errors.length > 0){ throw errors; } }); }); 
0
source share

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.

  /** * Class MyError extends Error but add the parentError attribute */ 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.

  /** * Class MyErrorPackage extends Error * but works like a error package */ 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 )); } 
0
source share

All Articles