How to set status code in response for errors? #1213
Replies: 1 comment
-
Pothos itself doesn't know anything about HTTP, and can't operate at the protocol level, so the answer here depends on the GraphQL server you are using. In general, the answer is that most server implementations support some form of error handling hook that allows you to customize how errors are handled in the response. The server implementation I am most familiar with is graphql-yoga, which allows setting http response codes based in extensions on GraphQLError instances: https://the-guild.dev/graphql/yoga-server/docs/features/error-masking#modifying-http-status-codes-and-headers If you are using the scope-auth plugin, you can customize the authorization errors using the Returning a const builder = new SchemaBuilder<{
Context: Context;
AuthScopes: YourScopeTypes,
}>({
scopeAuthOptions: {
treatErrorsAsUnauthorized: true,
unauthorizedError: (parent, context, info, result) => new GraphQLError(
`Not Authorized`,
{ extensions: { http: { status: 401 } }
)
},
plugins: [ScopeAuthPlugin],
authScopes: async (context) => ({
...scopes
}),
}); |
Beta Was this translation helpful? Give feedback.
-
Say I want to throw a
401: Unauthorized
error, how do I set the statusCode of the response to 401? Moreover, is there a way to programmatically control the statusCode?Beta Was this translation helpful? Give feedback.
All reactions