Created
March 20, 2018 03:24
-
-
Save bastengao/4de9bb287acedddd24e984c15b8cc7c6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
class CustomError < StandardError | |
# custom options to GraphQLError | |
def options | |
end | |
end | |
class MuationError < CustomError | |
def initialize(key) | |
@key = key | |
message = I18n.t(key, scope: 'mutations.errors') | |
end | |
end | |
class ModelError < CustomError | |
def initialize(key, record) | |
@key = key | |
@record = record | |
message = I18n.t(key, scope: 'models.errors') | |
end | |
end | |
raise MutationError, :login_error | |
raise ModelError, :invalid, record |
This file contains hidden or 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
zh-CN: | |
mutations: | |
errors: | |
login_error: 登录异常 | |
models: | |
errors: | |
product: | |
invalid: xx问题 |
This file contains hidden or 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
class Rescuable | |
def initialize(resolve_func) | |
@resolve_func = resolve_func | |
end | |
def call(obj, args, ctx) | |
@resolve_func.call(obj, args, ctx) | |
rescue CustomError => err | |
GraphQL::ExecutionError.new(err.message, options: err.options) | |
rescue ActiveRecord::RecordNotFound => err | |
detail = err.id ? " with id #{err.id}" : '' | |
GraphQL::ExecutionError.new("#{err.model} record not found#{detail}") | |
rescue ActiveRecord::RecordInvalid => err | |
GraphQL::ExecutionError.new(err.message) | |
rescue StandardError => err | |
GraphQL::ExecutionError.new("Unexpected error: #{err.message}") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment