deno_error.rs 14.4 KB
Newer Older
K
Kitson Kelly 已提交
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 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
use crate::diagnostics;
use crate::fmt_errors::JSErrorColor;
use crate::import_map;
pub use crate::msg::ErrorKind;
use crate::resolve_addr::ResolveAddrError;
use crate::source_maps::apply_source_map;
use crate::source_maps::SourceMapGetter;
use deno::JSError;
use hyper;
#[cfg(unix)]
use nix::{errno::Errno, Error as UnixError};
use std;
use std::fmt;
use std::io;
use std::str;
use url;

pub type DenoResult<T> = std::result::Result<T, DenoError>;

#[derive(Debug)]
pub struct DenoError {
  repr: Repr,
}

#[derive(Debug)]
enum Repr {
  Simple(ErrorKind, String),
  IoErr(io::Error),
  UrlErr(url::ParseError),
  HyperErr(hyper::Error),
  ImportMapErr(import_map::ImportMapError),
  Diagnostic(diagnostics::Diagnostic),
  JSError(JSError),
}

/// Create a new simple DenoError.
pub fn new(kind: ErrorKind, msg: String) -> DenoError {
  DenoError {
    repr: Repr::Simple(kind, msg),
  }
}

impl DenoError {
  pub fn kind(&self) -> ErrorKind {
    match self.repr {
      Repr::Simple(kind, ref _msg) => kind,
      // Repr::Simple(kind) => kind,
      Repr::IoErr(ref err) => {
        use std::io::ErrorKind::*;
        match err.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,
          WouldBlock => ErrorKind::WouldBlock,
          InvalidInput => ErrorKind::InvalidInput,
          InvalidData => ErrorKind::InvalidData,
          TimedOut => ErrorKind::TimedOut,
          Interrupted => ErrorKind::Interrupted,
          WriteZero => ErrorKind::WriteZero,
          Other => ErrorKind::Other,
          UnexpectedEof => ErrorKind::UnexpectedEof,
          _ => unreachable!(),
        }
      }
      Repr::UrlErr(ref err) => {
        use url::ParseError::*;
        match err {
          EmptyHost => ErrorKind::EmptyHost,
          IdnaError => ErrorKind::IdnaError,
          InvalidPort => ErrorKind::InvalidPort,
          InvalidIpv4Address => ErrorKind::InvalidIpv4Address,
          InvalidIpv6Address => ErrorKind::InvalidIpv6Address,
          InvalidDomainCharacter => ErrorKind::InvalidDomainCharacter,
          RelativeUrlWithoutBase => ErrorKind::RelativeUrlWithoutBase,
          RelativeUrlWithCannotBeABaseBase => {
            ErrorKind::RelativeUrlWithCannotBeABaseBase
          }
          SetHostOnCannotBeABaseUrl => ErrorKind::SetHostOnCannotBeABaseUrl,
          Overflow => ErrorKind::Overflow,
        }
      }
      Repr::HyperErr(ref err) => {
        // For some reason hyper::errors::Kind is private.
        if err.is_parse() {
          ErrorKind::HttpParse
        } else if err.is_user() {
          ErrorKind::HttpUser
        } else if err.is_canceled() {
          ErrorKind::HttpCanceled
        } else if err.is_closed() {
          ErrorKind::HttpClosed
        } else {
          ErrorKind::HttpOther
        }
      }
      Repr::ImportMapErr(ref _err) => ErrorKind::ImportMapError,
      Repr::Diagnostic(ref _err) => ErrorKind::Diagnostic,
      Repr::JSError(ref _err) => ErrorKind::JSError,
    }
  }

  pub fn apply_source_map<G: SourceMapGetter>(self, getter: &G) -> Self {
    if let Repr::JSError(js_error) = self.repr {
      return DenoError {
        repr: Repr::JSError(apply_source_map(&js_error, getter)),
      };
    } else {
      panic!("attempt to apply source map an unremappable error")
    }
  }
}

impl fmt::Display for DenoError {
  fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
    match self.repr {
      Repr::Simple(_kind, ref err_str) => f.pad(err_str),
      Repr::IoErr(ref err) => err.fmt(f),
      Repr::UrlErr(ref err) => err.fmt(f),
      Repr::HyperErr(ref err) => err.fmt(f),
      Repr::ImportMapErr(ref err) => f.pad(&err.msg),
      Repr::Diagnostic(ref err) => err.fmt(f),
      Repr::JSError(ref err) => JSErrorColor(err).fmt(f),
    }
  }
}

impl std::error::Error for DenoError {
  fn description(&self) -> &str {
    match self.repr {
      Repr::Simple(_kind, ref msg) => msg.as_str(),
      Repr::IoErr(ref err) => err.description(),
      Repr::UrlErr(ref err) => err.description(),
      Repr::HyperErr(ref err) => err.description(),
      Repr::ImportMapErr(ref err) => &err.msg,
      Repr::Diagnostic(ref err) => &err.items[0].message,
      Repr::JSError(ref err) => &err.description(),
    }
  }

  fn cause(&self) -> Option<&dyn std::error::Error> {
    match self.repr {
      Repr::Simple(_kind, ref _msg) => None,
      Repr::IoErr(ref err) => Some(err),
      Repr::UrlErr(ref err) => Some(err),
      Repr::HyperErr(ref err) => Some(err),
      Repr::ImportMapErr(ref _err) => None,
      Repr::Diagnostic(ref _err) => None,
      Repr::JSError(ref err) => Some(err),
    }
  }
}

impl From<io::Error> for DenoError {
  #[inline]
  fn from(err: io::Error) -> Self {
    Self {
      repr: Repr::IoErr(err),
    }
  }
}

impl From<url::ParseError> for DenoError {
  #[inline]
  fn from(err: url::ParseError) -> Self {
    Self {
      repr: Repr::UrlErr(err),
    }
  }
}

impl From<hyper::Error> for DenoError {
  #[inline]
  fn from(err: hyper::Error) -> Self {
    Self {
      repr: Repr::HyperErr(err),
    }
  }
}

impl From<ResolveAddrError> for DenoError {
  fn from(e: ResolveAddrError) -> Self {
    match e {
      ResolveAddrError::Syntax => Self {
        repr: Repr::Simple(
          ErrorKind::InvalidInput,
          "invalid address syntax".to_string(),
        ),
      },
      ResolveAddrError::Resolution(io_err) => Self {
        repr: Repr::IoErr(io_err),
      },
    }
  }
}

#[cfg(unix)]
impl From<UnixError> for DenoError {
  fn from(e: UnixError) -> Self {
    match e {
      UnixError::Sys(Errno::EPERM) => Self {
        repr: Repr::Simple(
          ErrorKind::PermissionDenied,
          Errno::EPERM.desc().to_owned(),
        ),
      },
      UnixError::Sys(Errno::EINVAL) => Self {
        repr: Repr::Simple(
          ErrorKind::InvalidInput,
          Errno::EINVAL.desc().to_owned(),
        ),
      },
      UnixError::Sys(Errno::ENOENT) => Self {
        repr: Repr::Simple(
          ErrorKind::NotFound,
          Errno::ENOENT.desc().to_owned(),
        ),
      },
      UnixError::Sys(err) => Self {
        repr: Repr::Simple(ErrorKind::UnixError, err.desc().to_owned()),
      },
      _ => Self {
        repr: Repr::Simple(ErrorKind::Other, format!("{}", e)),
      },
    }
  }
}

impl From<import_map::ImportMapError> for DenoError {
  fn from(err: import_map::ImportMapError) -> Self {
    Self {
      repr: Repr::ImportMapErr(err),
    }
  }
}

impl From<diagnostics::Diagnostic> for DenoError {
  fn from(diagnostic: diagnostics::Diagnostic) -> Self {
    Self {
      repr: Repr::Diagnostic(diagnostic),
    }
  }
}

impl From<JSError> for DenoError {
  fn from(err: JSError) -> Self {
    Self {
      repr: Repr::JSError(err),
    }
  }
}

pub fn bad_resource() -> DenoError {
  new(ErrorKind::BadResource, String::from("bad resource id"))
}

pub fn permission_denied() -> DenoError {
  new(
    ErrorKind::PermissionDenied,
    String::from("permission denied"),
  )
}

pub fn op_not_implemented() -> DenoError {
  new(
    ErrorKind::OpNotAvailable,
    String::from("op not implemented"),
  )
}

pub fn worker_init_failed() -> DenoError {
  // TODO(afinch7) pass worker error data through here
  new(
    ErrorKind::WorkerInitFailed,
    String::from("worker init failed"),
  )
}

pub fn no_buffer_specified() -> DenoError {
  new(ErrorKind::InvalidInput, String::from("no buffer specified"))
}

pub fn no_async_support() -> DenoError {
  new(
    ErrorKind::NoAsyncSupport,
    String::from("op doesn't support async calls"),
  )
}

pub fn no_sync_support() -> DenoError {
  new(
    ErrorKind::NoSyncSupport,
    String::from("op doesn't support sync calls"),
  )
}

pub fn err_check<R>(r: Result<R, DenoError>) {
  if let Err(e) = r {
    panic!(e.to_string());
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  use crate::ansi::strip_ansi_codes;
  use crate::diagnostics::Diagnostic;
  use crate::diagnostics::DiagnosticCategory;
  use crate::diagnostics::DiagnosticItem;
  use crate::import_map::ImportMapError;
  use deno::StackFrame;

  fn js_error() -> JSError {
    JSError {
      message: "Error: foo bar".to_string(),
      source_line: None,
      script_resource_name: None,
      line_number: None,
      start_position: None,
      end_position: None,
      error_level: None,
      start_column: None,
      end_column: None,
      frames: vec![
        StackFrame {
          line: 4,
          column: 16,
          script_name: "foo_bar.ts".to_string(),
          function_name: "foo".to_string(),
          is_eval: false,
          is_constructor: false,
          is_wasm: false,
        },
        StackFrame {
          line: 5,
          column: 20,
          script_name: "bar_baz.ts".to_string(),
          function_name: "qat".to_string(),
          is_eval: false,
          is_constructor: false,
          is_wasm: false,
        },
        StackFrame {
          line: 1,
          column: 1,
          script_name: "deno_main.js".to_string(),
          function_name: "".to_string(),
          is_eval: false,
          is_constructor: false,
          is_wasm: false,
        },
      ],
    }
  }

  fn diagnostic() -> Diagnostic {
    Diagnostic {
      items: vec![
        DiagnosticItem {
          message: "Example 1".to_string(),
          message_chain: None,
          code: 2322,
          category: DiagnosticCategory::Error,
          start_position: Some(267),
          end_position: Some(273),
          source_line: Some("  values: o => [".to_string()),
          line_number: Some(18),
          script_resource_name: Some(
            "deno/tests/complex_diagnostics.ts".to_string(),
          ),
          start_column: Some(2),
          end_column: Some(8),
          related_information: None,
        },
        DiagnosticItem {
          message: "Example 2".to_string(),
          message_chain: None,
          code: 2000,
          category: DiagnosticCategory::Error,
          start_position: Some(2),
          end_position: Some(2),
          source_line: Some("  values: undefined,".to_string()),
          line_number: Some(128),
          script_resource_name: Some("/foo/bar.ts".to_string()),
          start_column: Some(2),
          end_column: Some(8),
          related_information: None,
        },
      ],
    }
  }

  struct MockSourceMapGetter {}

  impl SourceMapGetter for MockSourceMapGetter {
    fn get_source_map(&self, _script_name: &str) -> Option<Vec<u8>> {
      Some(vec![])
    }

    fn get_source_line(
      &self,
      _script_name: &str,
      _line: usize,
    ) -> Option<String> {
      None
    }
  }

  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 = new(ErrorKind::NoError, "foo".to_string());
    assert_eq!(err.kind(), ErrorKind::NoError);
    assert_eq!(err.to_string(), "foo");
  }

  #[test]
  fn test_io_error() {
    let err = DenoError::from(io_error());
    assert_eq!(err.kind(), ErrorKind::NotFound);
    assert_eq!(err.to_string(), "entity not found");
  }

  #[test]
  fn test_url_error() {
    let err = DenoError::from(url_error());
    assert_eq!(err.kind(), ErrorKind::EmptyHost);
    assert_eq!(err.to_string(), "empty host");
  }

  // TODO find a way to easily test tokio errors and unix errors

  #[test]
  fn test_diagnostic() {
    let err = DenoError::from(diagnostic());
    assert_eq!(err.kind(), ErrorKind::Diagnostic);
    assert_eq!(strip_ansi_codes(&err.to_string()), "error TS2322: Example 1\n\n► deno/tests/complex_diagnostics.ts:19:3\n\n19   values: o => [\n     ~~~~~~\n\nerror TS2000: Example 2\n\n► /foo/bar.ts:129:3\n\n129   values: undefined,\n      ~~~~~~\n\n\nFound 2 errors.\n");
  }

  #[test]
  fn test_js_error() {
    let err = DenoError::from(js_error());
    assert_eq!(err.kind(), ErrorKind::JSError);
    assert_eq!(strip_ansi_codes(&err.to_string()), "error: Error: foo bar\n    at foo (foo_bar.ts:5:17)\n    at qat (bar_baz.ts:6:21)\n    at deno_main.js:2:2");
  }

  #[test]
  fn test_import_map_error() {
    let err = DenoError::from(import_map_error());
    assert_eq!(err.kind(), ErrorKind::ImportMapError);
    assert_eq!(err.to_string(), "an import map error");
  }

  #[test]
  fn test_bad_resource() {
    let err = bad_resource();
    assert_eq!(err.kind(), ErrorKind::BadResource);
    assert_eq!(err.to_string(), "bad resource id");
  }

  #[test]
  fn test_permission_denied() {
    let err = permission_denied();
    assert_eq!(err.kind(), ErrorKind::PermissionDenied);
    assert_eq!(err.to_string(), "permission denied");
  }

  #[test]
  fn test_op_not_implemented() {
    let err = op_not_implemented();
    assert_eq!(err.kind(), ErrorKind::OpNotAvailable);
    assert_eq!(err.to_string(), "op not implemented");
  }

  #[test]
  fn test_worker_init_failed() {
    let err = worker_init_failed();
    assert_eq!(err.kind(), ErrorKind::WorkerInitFailed);
    assert_eq!(err.to_string(), "worker init failed");
  }

  #[test]
  fn test_no_buffer_specified() {
    let err = no_buffer_specified();
    assert_eq!(err.kind(), ErrorKind::InvalidInput);
    assert_eq!(err.to_string(), "no buffer specified");
  }

  #[test]
  fn test_no_async_support() {
    let err = no_async_support();
    assert_eq!(err.kind(), ErrorKind::NoAsyncSupport);
    assert_eq!(err.to_string(), "op doesn't support async calls");
  }

  #[test]
  fn test_no_sync_support() {
    let err = no_sync_support();
    assert_eq!(err.kind(), ErrorKind::NoSyncSupport);
    assert_eq!(err.to_string(), "op doesn't support sync calls");
  }

  #[test]
  #[should_panic]
  fn test_apply_source_map_invalid() {
    let getter = MockSourceMapGetter {};
    let err = new(ErrorKind::NotFound, "not found".to_string());
    err.apply_source_map(&getter);
  }

  #[test]
  #[should_panic]
  fn test_err_check() {
    err_check(
      Err(new(ErrorKind::NotFound, "foo".to_string())) as Result<(), DenoError>
    );
  }
}