1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
// Copyright 2019 Palantir Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! 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 Rust
//! [`std::error::Error`] trait, or just a [`str`] or [`String`] containing a description of what happened. When
//! an [`Error`] is logged, the cause (and its chain of sources via [`std::error::Error::source`]) are included as a
//! parameter. The log-safety of that cause information is identified by the choice of constructor of the [`Error`].
//! * [`Error::safe_params`] and [`Error::unsafe_params`] - key-value pairs that can be added to the error to provide
//! context. When an [`Error`] is logged, these are included in the service log's parameters. When a service
//! [`Error`] is created, all of the parameters of its associated Conjure error are automatically included as
//! params, with [`ErrorType::safe_args`] used to partition the parameters between safe and unsafe. Additional
//! params can be added via [`Error::with_safe_param`] and [`Error::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 the [`Error`] is created, and additional backtraces can be added
//! with the [`Error::with_backtrace`] method. This can be used when, for example, an [`Error`] transfers from one
//! thread to another. When an [`Error`] 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 implementing
//! [`Serialize`] and [`ErrorType`]. 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 the [`Error::service`], [`Error::service_safe`], [`Error::internal`], and [`Error::internal_safe`]
//! functions.
//! * [`ErrorKind::Throttle`] - an indication that the client is making too many requests and should throttle
//! itself. This will generate a `429 Too Many Requests` HTTP response. Throttle errors are created with the
//! [`Error::throttle`], [`Error::throttle_safe`], [`Error::throttle_for`], and [`Error::throttle_for_safe`]
//! functions.
//! * [`ErrorKind::Unavailable`] - an indication that the server is unable to handle the request. This will
//! generate a `503 Service Unavailable` HTTP response. Unavailable errors are created with the
//! [`Error::unavailable`] and [`Error::unavailable_safe`] functions.
//!
//! [Conjure wire spec]: https://github.com/palantir/conjure/blob/master/docs/spec/wire.md#34-conjure-errors
//!
//! ## Examples
//!
//! Mapping a [`std::error::Error`] returned by a stdlib API into a generic internal service error:
//!
//! ```rust,no_run
//! use conjure_error::Error;
//! use std::fs::File;
//!
//! # fn foo() -> Result<(), Error> {
//! let file = File::open("var/data/database.csv").map_err(Error::internal_safe)?;
//! # Ok(()) }
//! ```
//!
//! Doing the same, but including the filename as an extra parameter:
//!
//! ```rust,no_run
//! use conjure_error::Error;
//! use std::fs::File;
//!
//! # fn foo() -> Result<(), Error> {
//! let filename = "var/data/database.csv";
//! let file = File::open(filename).map_err(|e| {
//! Error::internal_safe(e).with_safe_param("filename", filename)
//! })?;
//! # Ok(()) }
//! ```
//!
//! Returning a specific Conjure error when there is no existing error cause:
//!
//! ```yaml
//! types:
//! definitions:
//! errors:
//! ObjectNotFound:
//! namespace: MyService
//! code: INVALID_ARGUMENT
//! safe-args:
//! objectRid: rid
//! ```
//!
//! ```rust,ignore
//! 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)));
//! }
//! ```
extern crate self as conjure_error;
use Uuid;
use ;
use crate;
pub use crate*;
pub use crate*;
pub use crate*;
pub use crate*;
use DeserializeSeed;
/// A trait implemented by Conjure error types.
/// An `ErrorType` which wraps another and overrides its instance ID.
/// Encodes a Conjure error into its serialized form.
///
/// The error's instance ID will be randomly generated.
///
/// # Panics
///
/// Panics if the error type does not serialize as a struct.
/// Re-serializes the parameters of a [`SerializableError`] in the legacy stringified format.
///
/// Scalar parameters will be converted to their string representations and composite parameters
/// will be dropped.