mirror of
https://github.com/denoland/deno.git
synced 2024-11-24 15:19:26 -05:00
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.
This commit is contained in:
parent
8a6a2a50f7
commit
91f293442d
1 changed files with 27 additions and 29 deletions
|
@ -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::<PrettyJsError>() {
|
||||
serialized_error = json!({
|
||||
WorkerEvent::TerminalError(error) => match error.downcast::<JsError>() {
|
||||
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::<PrettyJsError>() {
|
||||
serialized_error = json!({
|
||||
Err(error) => {
|
||||
json!({
|
||||
"type": "terminalError",
|
||||
"error": {
|
||||
"message": error.to_string(),
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
WorkerEvent::Error(error) => match error.downcast::<JsError>() {
|
||||
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(),
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue