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

Configurable error messages #189

Open
TomasBarry opened this issue Jun 17, 2021 · 3 comments
Open

Configurable error messages #189

TomasBarry opened this issue Jun 17, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@TomasBarry
Copy link
Contributor

What is the problem the enhancement will solve?

When a token has expired, the error message returned is x field requires authentication. It would be great to be able to customise the error message and differentiate between the token being expired due to it having exceeded the lifespan, or whether it is invalidated due to previous usage.

Describe the solution you have in mind

Configurable error messages in general.

Describe alternatives you've considered

Writing custom logic in the controller to test the different scenarios and overload the response.

This seems a bit flakey though if the API of this gem were to change.

@00dav00 00dav00 added the needs triage Needs to be clasified by the mantiners label Jun 17, 2021
@00dav00
Copy link
Contributor

00dav00 commented Jun 17, 2021

Hi @TomasBarry , have you checked if the message you are trying to customize is among the translations file?
If so, you can overwrite it on your project.

@mcelicalderon
Copy link
Member

mcelicalderon commented Jun 22, 2021

Hey @TomasBarry! You can actually already customize this message if you are using the SchemaPlugin. Take a look at this lambda which is the default

DEFAULT_NOT_AUTHENTICATED = ->(field) { raise GraphqlDevise::AuthenticationError, "#{field} field requires authentication" }

You can initialize your SchemaPlugin with the unauthenticated_proc argument. Here you can pass any proc (actually any callable object) and the gem will call that proc with the field name where authentication failed as the only argument. Our default proc raises

class AuthenticationError < ExecutionError

that extends inherits from GraphQL::ExecutionError so an error is placed in the top level errors of the GQL response.

So, to be clear, you could do

class DummySchema < GraphQL::Schema
  use GraphqlDevise::SchemaPlugin.new(
    query:                Types::QueryType,
    mutation:             Types::MutationType,
    unauthenticated_proc: ->(field_name) { raise GraphqlDevise::AuthenticationError, "Custom error message for #{field_name}" }
  )
end

Now that I'm done writing, I realize this is not what you asked 🤣

Currently I don't see a way to differentiate why authentication failed (expiration, invalid, etc). I'll take a look and see if there's something we can do to customize that, but I'm thinking that if anything, it might have to happen at the controller level and not the GQL schema as we moved away from authenticating inside the schema.

@TomasBarry
Copy link
Contributor Author

@mcelicalderon you're spot on.

@mcelicalderon mcelicalderon added enhancement New feature or request and removed needs triage Needs to be clasified by the mantiners labels Aug 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants