Skip to content

Instantly share code, notes, and snippets.

@bastengao
Created March 20, 2018 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bastengao/4de9bb287acedddd24e984c15b8cc7c6 to your computer and use it in GitHub Desktop.
Save bastengao/4de9bb287acedddd24e984c15b8cc7c6 to your computer and use it in GitHub Desktop.
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
zh-CN:
mutations:
errors:
login_error: 登录异常
models:
errors:
product:
invalid: xx问题
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