-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from code0-tech/improve_error_models
Improve error models on api responses
- Loading branch information
Showing
25 changed files
with
202 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module Errors | ||
class ActiveModelErrorType < Types::BaseObject | ||
description 'Represents an active model error' | ||
|
||
field :attribute, String, null: false, description: 'The affected attribute on the model' | ||
field :type, String, null: false, description: 'The validation type that failed for the attribute' | ||
|
||
def type | ||
object.details[:error] | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module Errors | ||
class ErrorType < BaseUnion | ||
description 'Objects that can present an error' | ||
possible_types Errors::ActiveModelErrorType, Errors::MessageErrorType | ||
|
||
def self.resolve_type(object, _ctx) | ||
case object | ||
when ActiveModel::Error | ||
Errors::ActiveModelErrorType | ||
when Sagittarius::Graphql::ErrorMessageContainer | ||
Errors::MessageErrorType | ||
else | ||
raise 'Unsupported ErrorType' | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# frozen_string_literal: true | ||
|
||
module Types | ||
module Errors | ||
class MessageErrorType < Types::BaseObject | ||
description 'Represents an error message' | ||
|
||
field :message, String, null: false, description: 'The message provided from the error' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
--- | ||
title: ActiveModelError | ||
--- | ||
|
||
Represents an active model error | ||
|
||
## Fields without arguments | ||
|
||
| Name | Type | Description | | ||
|------|------|-------------| | ||
| `attribute` | [`String!`](../scalar/string.md) | The affected attribute on the model | | ||
| `type` | [`String!`](../scalar/string.md) | The validation type that failed for the attribute | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
title: MessageError | ||
--- | ||
|
||
Represents an error message | ||
|
||
## Fields without arguments | ||
|
||
| Name | Type | Description | | ||
|------|------|-------------| | ||
| `message` | [`String!`](../scalar/string.md) | The message provided from the error | | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
--- | ||
title: Error | ||
--- | ||
|
||
Objects that can present an error | ||
|
||
## Possible types | ||
|
||
- [`ActiveModelError`](../object/activemodelerror.md) | ||
- [`MessageError`](../object/messageerror.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# frozen_string_literal: true | ||
|
||
module Sagittarius | ||
module Graphql | ||
ErrorMessageContainer = Struct.new(:message) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe SagittariusSchema.types['ActiveModelError'] do | ||
let(:fields) do | ||
%w[ | ||
attribute | ||
type | ||
] | ||
end | ||
|
||
it { expect(described_class.graphql_name).to eq('ActiveModelError') } | ||
it { expect(described_class).to have_graphql_fields(fields) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe SagittariusSchema.types['Error'] do | ||
it 'returns possible types' do | ||
expect(described_class.possible_types).to include( | ||
Types::Errors::ActiveModelErrorType, | ||
Types::Errors::MessageErrorType | ||
) | ||
end | ||
|
||
describe '.resolve_type' do | ||
it 'resolves active model errors' do | ||
expect( | ||
described_class.resolve_type(ActiveModel::Error.new(nil, :test, :invalid), {}) | ||
).to eq(Types::Errors::ActiveModelErrorType) | ||
end | ||
|
||
it 'resolves message errors' do | ||
expect( | ||
described_class.resolve_type(Sagittarius::Graphql::ErrorMessageContainer.new(message: 'message'), {}) | ||
).to eq(Types::Errors::MessageErrorType) | ||
end | ||
|
||
it 'raises an error for invalid types' do | ||
expect { described_class.resolve_type(build(:user), {}) }.to raise_error 'Unsupported ErrorType' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'rails_helper' | ||
|
||
RSpec.describe SagittariusSchema.types['MessageError'] do | ||
let(:fields) do | ||
%w[ | ||
message | ||
] | ||
end | ||
|
||
it { expect(described_class.graphql_name).to eq('MessageError') } | ||
it { expect(described_class).to have_graphql_fields(fields) } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.