This directive allows to do one or both of the following actions:
-
Catch error thrown by the field/object type resolver and return some value instead. The directives allows to catch only certain errors using
catchError.condition
argument. -
Throw custom error instead of returning result of field/object type resolver. The directive allows to throw error only in certain cases using
throwError.condition
argument.
input CatchErrorInput {
condition: String
returnValue: JSON
}
input ThrowErrorInput {
condition: String
errorToThrow: String
}
directive @errorHandler(catchError: CatchErrorInput, throwError: ThrowErrorInput) on OBJECT | FIELD_DEFINITION
-
catchError
: (object, optional, default:undefined
) This argument has 2 properties and allows to configure what errors to catch and what value to returncondition
: (string, optional, default:true
) - this expression is evaluated using Argument injection mechanism. Additionally to the regularsource
,args
,context
andinfo
variables,error
variable is available. Its value is the error thrown by resolver.returnValue
(object, optional, default:null
) - object or string that may be evaluated using Argument injection mechanism. This value will be resolved if the error will be caught.
-
throwError
(object, optional, default:undefined
) - This argument has 2 properties and allows to configure what error will be thrown instead of resolver value.condition
: (string, optional, default:true
) - this expression is evaluated using Argument injection mechanism. Additionally to the regularsource
,args
,context
andinfo
variables,result
variable is available. Its value is the resolver value orcatchError
section value. See above.errorToThrow
: (string, optional, default: empty string). String that may be evaluated using Argument injection mechanism. If the evaluation result istypeof Error
it will be thrown. If the expression cannot be evaluated or is evaluated to stringnew Error(result)
will be thrown.
type Query {
foo: String @errorHandler
}
type Query {
foo: Foo @errorHandler(catchError: {})
}
type Query {
foo: String @errorHandler(catchError: { condition: "{ error.status === 404 }", returnValue: "Not found" })
}
type Query {
foo: String
@errorHandler(
throwError: { condition: "{ result.length === 0 }", errorToThrow: "{ new Error('404: Foo not found') }" }
)
}
type Query {
foo: String
@errorHandler(
catchError: { condition: "{ error.message.includes('Unauthorized) }", returnValue: "Not found" }
throwError: { condition: "{ result === 'Not found }", errorToThrow: "{ new Error('404: Foo not found') }" }
)
}