-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
op_error.rs
462 lines (409 loc) · 11.5 KB
/
op_error.rs
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
//! There are many types of errors in Deno:
//! - ErrBox: a generic boxed object. This is the super type of all
//! errors handled in Rust.
//! - JSError: exceptions thrown from V8 into Rust. Usually a user exception.
//! These are basically a big JSON structure which holds information about
//! line numbers. We use this to pretty-print stack traces. These are
//! never passed back into the runtime.
//! - OpError: these are errors that happen during ops, which are passed
//! back into the runtime, where an exception object is created and thrown.
//! OpErrors have an integer code associated with them - access this via the `kind` field.
//! - Diagnostic: these are errors that originate in TypeScript's compiler.
//! They're similar to JSError, in that they have line numbers.
//! But Diagnostics are compile-time type errors, whereas JSErrors are runtime exceptions.
//!
//! TODO:
//! - rename/merge JSError with V8Exception?
use crate::import_map::ImportMapError;
use deno_core::ErrBox;
use deno_core::ModuleResolutionError;
use dlopen;
use reqwest;
use rustyline::error::ReadlineError;
use std;
use std::env::VarError;
use std::error::Error;
use std::fmt;
use std::io;
use url;
// Warning! The values in this enum are duplicated in js/errors.ts
// Update carefully!
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum ErrorKind {
NotFound = 1,
PermissionDenied = 2,
ConnectionRefused = 3,
ConnectionReset = 4,
ConnectionAborted = 5,
NotConnected = 6,
AddrInUse = 7,
AddrNotAvailable = 8,
BrokenPipe = 9,
AlreadyExists = 10,
InvalidData = 13,
TimedOut = 14,
Interrupted = 15,
WriteZero = 16,
UnexpectedEof = 17,
BadResource = 18,
Http = 19,
URIError = 20,
TypeError = 21,
/// This maps to window.Error - ie. a generic error type
/// if no better context is available.
/// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error
Other = 22,
}
#[derive(Debug)]
pub struct OpError {
pub kind: ErrorKind,
pub msg: String,
}
impl OpError {
fn new(kind: ErrorKind, msg: String) -> Self {
Self { kind, msg }
}
pub fn not_found(msg: String) -> Self {
Self::new(ErrorKind::NotFound, msg)
}
pub fn other(msg: String) -> Self {
Self::new(ErrorKind::Other, msg)
}
pub fn type_error(msg: String) -> Self {
Self::new(ErrorKind::TypeError, msg)
}
pub fn http(msg: String) -> Self {
Self::new(ErrorKind::Http, msg)
}
pub fn uri_error(msg: String) -> Self {
Self::new(ErrorKind::URIError, msg)
}
pub fn permission_denied(msg: String) -> OpError {
Self::new(ErrorKind::PermissionDenied, msg)
}
pub fn bad_resource() -> OpError {
Self::new(ErrorKind::BadResource, "bad resource id".to_string())
}
}
impl Error for OpError {}
impl fmt::Display for OpError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.pad(self.msg.as_str())
}
}
impl From<ImportMapError> for OpError {
fn from(error: ImportMapError) -> Self {
OpError::from(&error)
}
}
impl From<&ImportMapError> for OpError {
fn from(error: &ImportMapError) -> Self {
Self {
kind: ErrorKind::Other,
msg: error.to_string(),
}
}
}
impl From<ModuleResolutionError> for OpError {
fn from(error: ModuleResolutionError) -> Self {
OpError::from(&error)
}
}
impl From<&ModuleResolutionError> for OpError {
fn from(error: &ModuleResolutionError) -> Self {
Self {
kind: ErrorKind::URIError,
msg: error.to_string(),
}
}
}
impl From<VarError> for OpError {
fn from(error: VarError) -> Self {
OpError::from(&error)
}
}
impl From<&VarError> for OpError {
fn from(error: &VarError) -> Self {
use VarError::*;
let kind = match error {
NotPresent => ErrorKind::NotFound,
NotUnicode(..) => ErrorKind::InvalidData,
};
Self {
kind,
msg: error.to_string(),
}
}
}
impl From<io::Error> for OpError {
fn from(error: io::Error) -> Self {
OpError::from(&error)
}
}
impl From<&io::Error> for OpError {
fn from(error: &io::Error) -> Self {
use io::ErrorKind::*;
let kind = match error.kind() {
NotFound => ErrorKind::NotFound,
PermissionDenied => ErrorKind::PermissionDenied,
ConnectionRefused => ErrorKind::ConnectionRefused,
ConnectionReset => ErrorKind::ConnectionReset,
ConnectionAborted => ErrorKind::ConnectionAborted,
NotConnected => ErrorKind::NotConnected,
AddrInUse => ErrorKind::AddrInUse,
AddrNotAvailable => ErrorKind::AddrNotAvailable,
BrokenPipe => ErrorKind::BrokenPipe,
AlreadyExists => ErrorKind::AlreadyExists,
InvalidInput => ErrorKind::TypeError,
InvalidData => ErrorKind::InvalidData,
TimedOut => ErrorKind::TimedOut,
Interrupted => ErrorKind::Interrupted,
WriteZero => ErrorKind::WriteZero,
UnexpectedEof => ErrorKind::UnexpectedEof,
Other => ErrorKind::Other,
WouldBlock => unreachable!(),
// Non-exhaustive enum - might add new variants
// in the future
_ => unreachable!(),
};
Self {
kind,
msg: error.to_string(),
}
}
}
impl From<url::ParseError> for OpError {
fn from(error: url::ParseError) -> Self {
OpError::from(&error)
}
}
impl From<&url::ParseError> for OpError {
fn from(error: &url::ParseError) -> Self {
Self {
kind: ErrorKind::URIError,
msg: error.to_string(),
}
}
}
impl From<reqwest::Error> for OpError {
fn from(error: reqwest::Error) -> Self {
OpError::from(&error)
}
}
impl From<&reqwest::Error> for OpError {
fn from(error: &reqwest::Error) -> Self {
match error.source() {
Some(err_ref) => None
.or_else(|| {
err_ref
.downcast_ref::<url::ParseError>()
.map(|e| e.clone().into())
})
.or_else(|| {
err_ref
.downcast_ref::<io::Error>()
.map(|e| e.to_owned().into())
})
.or_else(|| {
err_ref
.downcast_ref::<serde_json::error::Error>()
.map(|e| e.into())
})
.unwrap_or_else(|| Self {
kind: ErrorKind::Http,
msg: error.to_string(),
}),
None => Self {
kind: ErrorKind::Http,
msg: error.to_string(),
},
}
}
}
impl From<ReadlineError> for OpError {
fn from(error: ReadlineError) -> Self {
OpError::from(&error)
}
}
impl From<&ReadlineError> for OpError {
fn from(error: &ReadlineError) -> Self {
use ReadlineError::*;
let kind = match error {
Io(err) => return err.into(),
Eof => ErrorKind::UnexpectedEof,
Interrupted => ErrorKind::Interrupted,
#[cfg(unix)]
Errno(err) => return err.into(),
_ => unimplemented!(),
};
Self {
kind,
msg: error.to_string(),
}
}
}
impl From<serde_json::error::Error> for OpError {
fn from(error: serde_json::error::Error) -> Self {
OpError::from(&error)
}
}
impl From<&serde_json::error::Error> for OpError {
fn from(error: &serde_json::error::Error) -> Self {
use serde_json::error::*;
let kind = match error.classify() {
Category::Io => ErrorKind::TypeError,
Category::Syntax => ErrorKind::TypeError,
Category::Data => ErrorKind::InvalidData,
Category::Eof => ErrorKind::UnexpectedEof,
};
Self {
kind,
msg: error.to_string(),
}
}
}
#[cfg(unix)]
mod unix {
use super::{ErrorKind, OpError};
use nix::errno::Errno::*;
pub use nix::Error;
use nix::Error::Sys;
impl From<Error> for OpError {
fn from(error: Error) -> Self {
OpError::from(&error)
}
}
impl From<&Error> for OpError {
fn from(error: &Error) -> Self {
let kind = match error {
Sys(EPERM) => ErrorKind::PermissionDenied,
Sys(EINVAL) => ErrorKind::TypeError,
Sys(ENOENT) => ErrorKind::NotFound,
Sys(UnknownErrno) => unreachable!(),
Sys(_) => unreachable!(),
Error::InvalidPath => ErrorKind::TypeError,
Error::InvalidUtf8 => ErrorKind::InvalidData,
Error::UnsupportedOperation => unreachable!(),
};
Self {
kind,
msg: error.to_string(),
}
}
}
}
impl From<dlopen::Error> for OpError {
fn from(error: dlopen::Error) -> Self {
OpError::from(&error)
}
}
impl From<&dlopen::Error> for OpError {
fn from(error: &dlopen::Error) -> Self {
use dlopen::Error::*;
let kind = match error {
NullCharacter(_) => ErrorKind::Other,
OpeningLibraryError(e) => return e.into(),
SymbolGettingError(e) => return e.into(),
AddrNotMatchingDll(e) => return e.into(),
NullSymbol => ErrorKind::Other,
};
Self {
kind,
msg: error.to_string(),
}
}
}
impl From<ErrBox> for OpError {
fn from(error: ErrBox) -> Self {
#[cfg(unix)]
fn unix_error_kind(err: &ErrBox) -> Option<OpError> {
err.downcast_ref::<unix::Error>().map(|e| e.into())
}
#[cfg(not(unix))]
fn unix_error_kind(_: &ErrBox) -> Option<OpError> {
None
}
None
.or_else(|| {
error
.downcast_ref::<OpError>()
.map(|e| OpError::new(e.kind, e.msg.to_string()))
})
.or_else(|| error.downcast_ref::<reqwest::Error>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<ImportMapError>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<io::Error>().map(|e| e.into()))
.or_else(|| {
error
.downcast_ref::<ModuleResolutionError>()
.map(|e| e.into())
})
.or_else(|| error.downcast_ref::<url::ParseError>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<VarError>().map(|e| e.into()))
.or_else(|| error.downcast_ref::<ReadlineError>().map(|e| e.into()))
.or_else(|| {
error
.downcast_ref::<serde_json::error::Error>()
.map(|e| e.into())
})
.or_else(|| error.downcast_ref::<dlopen::Error>().map(|e| e.into()))
.or_else(|| unix_error_kind(&error))
.unwrap_or_else(|| {
panic!("Can't downcast {:?} to OpError", error);
})
}
}
#[cfg(test)]
mod tests {
use super::*;
fn io_error() -> io::Error {
io::Error::from(io::ErrorKind::NotFound)
}
fn url_error() -> url::ParseError {
url::ParseError::EmptyHost
}
fn import_map_error() -> ImportMapError {
ImportMapError {
msg: "an import map error".to_string(),
}
}
#[test]
fn test_simple_error() {
let err = OpError::not_found("foo".to_string());
assert_eq!(err.kind, ErrorKind::NotFound);
assert_eq!(err.to_string(), "foo");
}
#[test]
fn test_io_error() {
let err = OpError::from(io_error());
assert_eq!(err.kind, ErrorKind::NotFound);
assert_eq!(err.to_string(), "entity not found");
}
#[test]
fn test_url_error() {
let err = OpError::from(url_error());
assert_eq!(err.kind, ErrorKind::URIError);
assert_eq!(err.to_string(), "empty host");
}
// TODO find a way to easily test tokio errors and unix errors
#[test]
fn test_import_map_error() {
let err = OpError::from(import_map_error());
assert_eq!(err.kind, ErrorKind::Other);
assert_eq!(err.to_string(), "an import map error");
}
#[test]
fn test_bad_resource() {
let err = OpError::bad_resource();
assert_eq!(err.kind, ErrorKind::BadResource);
assert_eq!(err.to_string(), "bad resource id");
}
#[test]
fn test_permission_denied() {
let err = OpError::permission_denied(
"run again with the --allow-net flag".to_string(),
);
assert_eq!(err.kind, ErrorKind::PermissionDenied);
assert_eq!(err.to_string(), "run again with the --allow-net flag");
}
}