Expand description
Runtime support for Conjure error types.
In a networked service, the error objects that are propagated through its codebase are responsible for two things:
- Collecting useful information that a developer can use to diagnose whatever problem caused the error.
- Controlling how the error is presented to the client.
Services implemented using Conjure’s frameworks use the Error
type defined in this crate as the single error
type throughout the codebase. Error
s store:
- Developer facing:
Error::cause
- the underlying cause of the error. This can be a type implementing the Ruststd::error::Error
trait, or just astr
orString
containing a description of what happened. When anError
is logged, the cause (and its chain of sources viastd::error::Error::source
) are included as a parameter. The log-safety of that cause information is identified by the choice of constructor of theError
.Error::safe_params
andError::unsafe_params
- key-value pairs that can be added to the error to provide context. When anError
is logged, these are included in the service log’s parameters. When a serviceError
is created, all of the parameters of its associated Conjure error are automatically included as params, withErrorType::safe_args
used to partition the parameters between safe and unsafe. Additional params can be added viaError::with_safe_param
andError::with_unsafe_param
.Error::backtraces
- a sequence of backtraces to annotate the error with the state of the function call stack. A backtrace is automatically taken when theError
is created, and additional backtraces can be added with theError::with_backtrace
method. This can be used when, for example, anError
transfers from one thread to another. When anError
is logged, its backtraces will be included in the stacktrace field.
- Client facing:
Error::kind
- how the error should be reported to the client. There are currently three kinds:ErrorKind::Service
- a standard service error. These are constructed from a type implementingSerialize
andErrorType
. The value is expected to serialize as a struct, with the struct’s fields being the parameters of the error. Errors defined in Conjure APIs will generate types implementing these traits. This will generate an HTTP response following the Conjure wire spec. Service errors are created with theError::service
,Error::service_safe
,Error::internal
, andError::internal_safe
functions.ErrorKind::Throttle
- an indication that the client is making too many requests and should throttle itself. This will generate a429 Too Many Requests
HTTP response. Throttle errors are created with theError::throttle
,Error::throttle_safe
,Error::throttle_for
, andError::throttle_for_safe
functions.ErrorKind::Unavailable
- an indication that the server is unable to handle the request. This will generate a503 Service Unavailable
HTTP response. Unavailable errors are created with theError::unavailable
andError::unavailable_safe
functions.
§Examples
Mapping a std::error::Error
returned by a stdlib API into a generic internal service error:
use conjure_error::Error;
use std::fs::File;
let file = File::open("var/data/database.csv").map_err(Error::internal_safe)?;
Doing the same, but including the filename as an extra parameter:
use conjure_error::Error;
use std::fs::File;
let filename = "var/data/database.csv";
let file = File::open(filename).map_err(|e| {
Error::internal_safe(e).with_safe_param("filename", filename)
})?;
Returning a specific Conjure error when there is no existing error cause:
types:
definitions:
errors:
ObjectNotFound:
namespace: MyService
code: INVALID_ARGUMENT
safe-args:
objectRid: rid
ⓘ
use conjure_error::Error;
use my_service_api::errors::ObjectNotFound;
if !object_was_found {
return Err(Error::service_safe("failed to find object", ObjectNotFound::new(object_rid)));
}
Modules§
- conflict
- error_
code - failed_
precondition - internal
- invalid_
argument - not_
found - permission_
denied - request_
entity_ too_ large - serializable_
error - timeout
Structs§
- Backtrace
- A backtrace associated with an
Error
. - Conflict
- A generic
CONFLICT
error. - Error
- A standard error type for network services.
- Failed
Precondition - A generic
FAILED_PRECONDITION
error. - Internal
- A generic
INTERNAL
error. - Invalid
Argument - A generic
INVALID_ARGUMENT
error. - NotFound
- A generic
NOT_FOUND
error. - Params
- A collection of error parameters, either safe or unsafe.
- Params
Iter - An iterator over the parameters of an error.
- Permission
Denied - A generic
PERMISSION_DENIED
error. - Request
Entity TooLarge - A generic
REQUEST_ENTITY_TOO_LARGE
error. - Serializable
Error - The JSON-serializable representation of an error.
- Throttle
Error - Information about a throttle error.
- Timeout
- A generic
TIMEOUT
error. - Unavailable
Error - Information about an unavailable error.
- With
Instance Id - An
ErrorType
which wraps another and overrides its instance ID.
Enums§
- Error
Code - The broad category of a Conjure error.
- Error
Kind - Information about the specific type of an
Error
.
Traits§
- Error
Type - A trait implemented by Conjure error types.
Functions§
- encode
- Encodes a Conjure error into its serialized form.
- stringify_
parameters - Re-serializes the parameters of a
SerializableError
in the legacy stringified format.