Skip to content

Commit

Permalink
Merge pull request #101 from graphql-devise/error-code-authentication…
Browse files Browse the repository at this point in the history
…-error

Change authentication error code
  • Loading branch information
mcelicalderon committed Jun 12, 2020
2 parents 5d2d437 + 02efdde commit 54155fe
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 19 deletions.
14 changes: 8 additions & 6 deletions lib/graphql_devise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ def self.add_mapping(mapping_name, resource)
end
end

require 'graphql_devise/engine'
require 'graphql_devise/version'
require 'graphql_devise/errors/error_codes'
require 'graphql_devise/errors/execution_error'
require 'graphql_devise/errors/user_error'
require 'graphql_devise/errors/authentication_error'
require 'graphql_devise/errors/detailed_user_error'

require 'graphql_devise/concerns/controller_methods'
require 'graphql_devise/schema'
require 'graphql_devise/types/authenticatable_type'
Expand All @@ -46,12 +54,6 @@ def self.add_mapping(mapping_name, resource)
require 'graphql_devise/default_operations/resolvers'
require 'graphql_devise/resolvers/dummy'

require 'graphql_devise/engine'
require 'graphql_devise/version'
require 'graphql_devise/error_codes'
require 'graphql_devise/user_error'
require 'graphql_devise/detailed_user_error'

require 'graphql_devise/mount_method/option_sanitizer'
require 'graphql_devise/mount_method/options_validator'
require 'graphql_devise/mount_method/operation_preparer'
Expand Down
5 changes: 0 additions & 5 deletions lib/graphql_devise/error_codes.rb

This file was deleted.

7 changes: 7 additions & 0 deletions lib/graphql_devise/errors/authentication_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module GraphqlDevise
class AuthenticationError < ExecutionError
def to_h
super.merge(extensions: { code: ERROR_CODES.fetch(:authentication_error) })
end
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GraphqlDevise
class DetailedUserError < GraphQL::ExecutionError
class DetailedUserError < ExecutionError
def initialize(message, errors:)
@message = message
@errors = errors
Expand Down
6 changes: 6 additions & 0 deletions lib/graphql_devise/errors/error_codes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module GraphqlDevise
ERROR_CODES = {
user_error: 'USER_ERROR',
authentication_error: 'AUTHENTICATION_ERROR'
}.freeze
end
4 changes: 4 additions & 0 deletions lib/graphql_devise/errors/execution_error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module GraphqlDevise
class ExecutionError < GraphQL::ExecutionError
end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module GraphqlDevise
class UserError < GraphQL::ExecutionError
class UserError < ExecutionError
def to_h
super.merge(extensions: { code: ERROR_CODES.fetch(:user_error) })
end
Expand Down
2 changes: 1 addition & 1 deletion lib/graphql_devise/schema_plugin.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module GraphqlDevise
class SchemaPlugin
DEFAULT_NOT_AUTHENTICATED = ->(field) { raise GraphqlDevise::UserError, "#{field} field requires authentication" }
DEFAULT_NOT_AUTHENTICATED = ->(field) { raise GraphqlDevise::AuthenticationError, "#{field} field requires authentication" }

def initialize(query: nil, mutation: nil, authenticate_default: true, resource_loaders: [], unauthenticated_proc: DEFAULT_NOT_AUTHENTICATED)
@query = query
Expand Down
10 changes: 5 additions & 5 deletions spec/requests/user_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
context 'when user is not authenticated' do
it 'returns a must sign in error' do
expect(json_response[:errors]).to contain_exactly(
hash_including(message: 'privateField field requires authentication', extensions: { code: 'USER_ERROR' })
hash_including(message: 'privateField field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' })
)
end
end
Expand All @@ -74,7 +74,7 @@
context 'when user is not authenticated' do
it 'returns a must sign in error' do
expect(json_response[:errors]).to contain_exactly(
hash_including(message: 'privateField field requires authentication', extensions: { code: 'USER_ERROR' })
hash_including(message: 'privateField field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' })
)
end
end
Expand Down Expand Up @@ -104,7 +104,7 @@
context 'when user is not authenticated' do
it 'returns a must sign in error' do
expect(json_response[:errors]).to contain_exactly(
hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'USER_ERROR' })
hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' })
)
end
end
Expand All @@ -124,7 +124,7 @@
context 'when user is not authenticated' do
it 'returns a must sign in error' do
expect(json_response[:errors]).to contain_exactly(
hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'USER_ERROR' })
hash_including(message: 'dummyMutation field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' })
)
end
end
Expand Down Expand Up @@ -162,7 +162,7 @@
context 'when user is not authenticated' do
it 'returns a must sign in error' do
expect(json_response[:errors]).to contain_exactly(
hash_including(message: 'user field requires authentication', extensions: { code: 'USER_ERROR' })
hash_including(message: 'user field requires authentication', extensions: { code: 'AUTHENTICATION_ERROR' })
)
end
end
Expand Down

0 comments on commit 54155fe

Please sign in to comment.