Skip to content
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

Create a general error formateer #1510

Open
karlos1337 opened this issue Apr 9, 2024 · 0 comments
Open

Create a general error formateer #1510

karlos1337 opened this issue Apr 9, 2024 · 0 comments
Labels
question Further information is requested

Comments

@karlos1337
Copy link

karlos1337 commented Apr 9, 2024

Is there any way to centralize every error format for the whole schema? I'm guessing about a middleware similar to:

fn error_formateer(error_info: ErrorValues, next: NextErrorFormateerRequest<'_>) {
  match error_info.type {
     MyDbORM::Error(error) => async_graphql::ValidationError::new("Validation Error")
       .extend_with(|_, e| e.set("path", error_info.field_path)
       .extend_with(|_, e| e.set("code", error_info.code)
    async_graphql::ValidationError(error) => error.extend_with(|_, e| e.set("from", "gql")
        .extend_with(|_, e| e.set("code",  error_info.code)
        .extend_with(|_, e| e.set("path", error_info.field_path)
  }
}

I hope to be able to send something concise to the client, like:

{
   ...
   "extensions": {
      "code": "INVALID_EMAIL"
      "message": "Field is not an email",
      "path": "userInput.email"
   }
   ...
   "extensions": {
      "code": "MIN_LENGH",
      "message": "Field lenght is less than minimal",
      "minLenght": 10,
      "path": "userInput.name"
    }
}

But not being forced to manually handle the same validation errors repeatedly on each resolver.

I've tried using validation middleware, but the error is already formatted, and it's hard to obtain error granular info and a unique code for each error.

@karlos1337 karlos1337 added the question Further information is requested label Apr 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant