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. Errors store:
- Developer facing:
Error::cause- the underlying cause of the error. This can be a type implementing the Ruststd::error::Errortrait, or just astrorStringcontaining a description of what happened. When anErroris 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_paramsandError::unsafe_params- key-value pairs that can be added to the error to provide context. When anErroris logged, these are included in the service log’s parameters. When a serviceErroris created, all of the parameters of its associated Conjure error are automatically included as params, withErrorType::safe_argsused to partition the parameters between safe and unsafe. Additional params can be added viaError::with_safe_paramandError::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 theErroris created, and additional backtraces can be added with theError::with_backtracemethod. This can be used when, for example, anErrortransfers from one thread to another. When anErroris 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 implementingSerializeandErrorType. 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_safefunctions.ErrorKind::Throttle- an indication that the client is making too many requests and should throttle itself. This will generate a429 Too Many RequestsHTTP response. Throttle errors are created with theError::throttle,Error::throttle_safe,Error::throttle_for, andError::throttle_for_safefunctions.ErrorKind::Unavailable- an indication that the server is unable to handle the request. This will generate a503 Service UnavailableHTTP response. Unavailable errors are created with theError::unavailableandError::unavailable_safefunctions.
§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
CONFLICTerror. - Error
- A standard error type for network services.
- Failed
Precondition - A generic
FAILED_PRECONDITIONerror. - Internal
- A generic
INTERNALerror. - Invalid
Argument - A generic
INVALID_ARGUMENTerror. - NotFound
- A generic
NOT_FOUNDerror. - 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_DENIEDerror. - Request
Entity TooLarge - A generic
REQUEST_ENTITY_TOO_LARGEerror. - Serializable
Error - The JSON-serializable representation of an error.
- Throttle
Error - Information about a throttle error.
- Timeout
- A generic
TIMEOUTerror. - Unavailable
Error - Information about an unavailable error.
- With
Instance Id - An
ErrorTypewhich 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
SerializableErrorin the legacy stringified format.