From 91f293442d5bfb28212b5b13b536ceae06427c99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartek=20Iwa=C5=84czuk?= Date: Fri, 20 Nov 2020 13:25:18 +0100 Subject: [PATCH] refactor(cli): worker event serialization (#8438) This commit simplifies code responsible for serialization of web worker events. Instead of using "PrettyJsError" it's enough to downcast to "deno_core::JsError", making the code less specific to cli/ implementation. --- cli/ops/worker_host.rs | 56 ++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/cli/ops/worker_host.rs b/cli/ops/worker_host.rs index 331ff8a575..e4e0d84a48 100644 --- a/cli/ops/worker_host.rs +++ b/cli/ops/worker_host.rs @@ -1,7 +1,6 @@ // Copyright 2018-2020 the Deno authors. All rights reserved. MIT license. use crate::colors; -use crate::fmt_errors::PrettyJsError; use crate::ops::io::get_stdio; use crate::permissions::Permissions; use crate::program_state::ProgramState; @@ -11,6 +10,7 @@ use crate::worker::WebWorkerHandle; use crate::worker::WorkerEvent; use deno_core::error::generic_error; use deno_core::error::AnyError; +use deno_core::error::JsError; use deno_core::futures::channel::mpsc; use deno_core::futures::future::FutureExt; use deno_core::serde_json; @@ -293,16 +293,9 @@ fn op_host_terminate_worker( fn serialize_worker_event(event: WorkerEvent) -> Value { match event { WorkerEvent::Message(buf) => json!({ "type": "msg", "data": buf }), - WorkerEvent::TerminalError(error) => { - let mut serialized_error = json!({ - "type": "terminalError", - "error": { - "message": error.to_string(), - } - }); - - if let Ok(js_error) = error.downcast::() { - serialized_error = json!({ + WorkerEvent::TerminalError(error) => match error.downcast::() { + Ok(js_error) => { + json!({ "type": "terminalError", "error": { "message": js_error.message, @@ -310,21 +303,20 @@ fn serialize_worker_event(event: WorkerEvent) -> Value { "lineNumber": js_error.line_number, "columnNumber": js_error.start_column, } - }); + }) } - - serialized_error - } - WorkerEvent::Error(error) => { - let mut serialized_error = json!({ - "type": "error", - "error": { - "message": error.to_string(), - } - }); - - if let Ok(js_error) = error.downcast::() { - serialized_error = json!({ + Err(error) => { + json!({ + "type": "terminalError", + "error": { + "message": error.to_string(), + } + }) + } + }, + WorkerEvent::Error(error) => match error.downcast::() { + Ok(js_error) => { + json!({ "type": "error", "error": { "message": js_error.message, @@ -332,11 +324,17 @@ fn serialize_worker_event(event: WorkerEvent) -> Value { "lineNumber": js_error.line_number, "columnNumber": js_error.start_column, } - }); + }) } - - serialized_error - } + Err(error) => { + json!({ + "type": "error", + "error": { + "message": error.to_string(), + } + }) + } + }, } }