in Education by
I am working in an graphql application where I have to send custom error object / message in json irrespective of whether it occurs in servlet or service. Expected error response { errorCode: 400 //error goes here, errorMessage: "my error mesage"} It will be helpful if someone could guide me to achieve the above requirement. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
GraphQL specification defines a clear format for the error entry in the response. According to the spec, it should like this (assuming JSON format is used): "errors": [ { "message": "Name for character with ID 1002 could not be fetched.", "locations": [ { "line": 6, "column": 7 } ], "path": [ "hero", "heroFriends", 1, "name" ] "extensions": {/* You can place data in any format here */} } ] So you won't find a GraphQL implementation that allows you to extend it and return some like this in the GraphQL execution result, for example: "errors": [ { "errorMessage": "Name for character with ID 1002 could not be fetched.", "errorCode": 404 } ] However, the spec lets you add data in whatever format in the extension entry. So you could create a custom Exception on the server side and end up with a response that looks like this in JSON: "errors": [ { "message": "Name for character with ID 1002 could not be fetched.", "locations": [ { "line": 6, "column": 7 } ], "path": [ "hero", "heroFriends", 1, "name" ] "extensions": { "errorMessage": "Name for character with ID 1002 could not be fetched.", "errorCode": 404 } } ] It's quite easy to implement this on GraphQL Java, as described in the docs. You can create a custom exception that overrides the getExtensions method and create a map inside the implementation that will then be used to build the content of extensions: public class CustomException extends RuntimeException implements GraphQLError { private final int errorCode; public CustomException(int errorCode, String errorMessage) { super(errorMessage); this.errorCode = errorCode; } @Override public Map getExtensions() { Map customAttributes = new LinkedHashMap<>(); customAttributes.put("errorCode", this.errorCode); customAttributes.put("errorMessage", this.getMessage()); return customAttributes; } @Override public List getLocations() { return null; } @Override public ErrorType getErrorType() { return null; } } then you can throw the exception passing in the code and message from inside your data fetchers: throw new CustomException(400, "A custom error message"); Now, there is another way to tackle this. Assuming you are working on a Web application, you can return errors (and data, for that matter) in whatever format that you want. Although that is a bit awkward in my opinion. GraphQL clients, like Apollo, adhere to the spec, so why would you want to return a response on any other format? But anyway, there are lots of different requirements out there. Once you get a hold of an ExecutionResult, you can create a map or object in whatever format you want, serialise that as JSON and return this over HTTP. Map result = new HashMap<>(); result.put("data", executionResult.getData()); List
> errors = executionResult.getErrors() .stream() .map(error -> { Map errorMap = new HashMap<>(); errorMap.put("errorMessage", error.getMessage()); errorMap.put("errorCode", 404); // get the code somehow from the error object return errorMap; }) .collect(toList()); result.put("errors", errors); // Serialize "result" and return that. But again, having a response that doesn't comply with the spec doesn't make sense in most of the cases.

Related questions

0 votes
    I'm trying to query two JSON files that I put in a src/data directory. I installed gatsby- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 4, 2022 in Education by JackTerrance
0 votes
    I'm trying to query two JSON files that I put in a src/data directory. I installed gatsby- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 24, 2022 in Education by JackTerrance
0 votes
    I am trying to fetch some data from the GitHub GraphQL but I get a GaphQLError. I have tried the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 19, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to solve this java error? java.io.IOException: Invalid keystore format I get it when ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    Does anyone know how to solve this java error? java.io.IOException: Invalid keystore format I get it when ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 16, 2022 in Education by JackTerrance
0 votes
    I am working on my first Android Application. What I am trying to do is a POST request to a REST ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I want to send the following JSON text {"Email":"[email protected]","Password":"123456"} to a web ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I been receiving date in the "yyyy-MM-dd'T'HH:mm:ss.SSSXXX" format from JSON input, which gets ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    here is my code. var http=require("http"); var fs = require("fs"); var express = require(" ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 19, 2022 in Education by JackTerrance
0 votes
    How to format a Microsoft JSON date?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    How to display JSON in an easy-to-read (for human readers) format?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    How to pass json format data on ajax call?...
asked Dec 10, 2020 in Technology by JackTerrance
0 votes
    I am working on wordpress with woocommerce and using WCK plugin for custom fields. I am creating products ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 18, 2022 in Education by JackTerrance
0 votes
    I have a DropDownList with values that I get from a SQL database. Depending on the selection made from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 7, 2022 in Education by JackTerrance
0 votes
    I have a DropDownList with values that I get from a SQL database. Depending on the selection made from ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 5, 2022 in Education by JackTerrance
...