mirror of
https://github.com/denoland/deno.git
synced 2025-08-04 02:48:24 +00:00
Encode op errors as strings instead of numbers (#6977)
This commit is contained in:
parent
479164d287
commit
59ca66a207
14 changed files with 197 additions and 293 deletions
|
@ -27,7 +27,7 @@ pub enum JsonOp {
|
|||
fn json_err(err: OpError) -> Value {
|
||||
json!({
|
||||
"message": err.msg,
|
||||
"kind": err.kind as u32,
|
||||
"kind": err.kind_str,
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -40,21 +40,24 @@ impl Into<Buf> for Record {
|
|||
pub struct ErrorRecord {
|
||||
pub promise_id: i32,
|
||||
pub arg: i32,
|
||||
pub error_code: i32,
|
||||
pub error_len: i32,
|
||||
pub error_code: Vec<u8>,
|
||||
pub error_message: Vec<u8>,
|
||||
}
|
||||
|
||||
impl Into<Buf> for ErrorRecord {
|
||||
fn into(self) -> Buf {
|
||||
let v32: Vec<i32> = vec![self.promise_id, self.arg, self.error_code];
|
||||
let v32: Vec<i32> = vec![self.promise_id, self.arg, self.error_len];
|
||||
let mut v8: Vec<u8> = Vec::new();
|
||||
for n in v32 {
|
||||
v8.write_i32::<LittleEndian>(n).unwrap();
|
||||
}
|
||||
let mut code = self.error_code;
|
||||
let mut message = self.error_message;
|
||||
// Align to 32bit word, padding with the space character.
|
||||
message.resize((message.len() + 3usize) & !3usize, b' ');
|
||||
v8.append(&mut code);
|
||||
v8.append(&mut message);
|
||||
// Align to 32bit word, padding with the space character.
|
||||
v8.resize((v8.len() + 3usize) & !3usize, b' ');
|
||||
v8.into_boxed_slice()
|
||||
}
|
||||
}
|
||||
|
@ -62,13 +65,14 @@ impl Into<Buf> for ErrorRecord {
|
|||
#[test]
|
||||
fn test_error_record() {
|
||||
let expected = vec![
|
||||
1, 0, 0, 0, 255, 255, 255, 255, 10, 0, 0, 0, 69, 114, 114, 111, 114, 32,
|
||||
32, 32,
|
||||
1, 0, 0, 0, 255, 255, 255, 255, 11, 0, 0, 0, 66, 97, 100, 82, 101, 115,
|
||||
111, 117, 114, 99, 101, 69, 114, 114, 111, 114,
|
||||
];
|
||||
let err_record = ErrorRecord {
|
||||
promise_id: 1,
|
||||
arg: -1,
|
||||
error_code: 10,
|
||||
error_len: 11,
|
||||
error_code: "BadResource".to_string().as_bytes().to_owned(),
|
||||
error_message: "Error".to_string().as_bytes().to_owned(),
|
||||
};
|
||||
let buf: Buf = err_record.into();
|
||||
|
@ -128,7 +132,8 @@ where
|
|||
let error_record = ErrorRecord {
|
||||
promise_id: 0,
|
||||
arg: -1,
|
||||
error_code: e.kind as i32,
|
||||
error_len: e.kind_str.len() as i32,
|
||||
error_code: e.kind_str.as_bytes().to_owned(),
|
||||
error_message: e.msg.as_bytes().to_owned(),
|
||||
};
|
||||
return Op::Sync(error_record.into());
|
||||
|
@ -148,7 +153,8 @@ where
|
|||
let error_record = ErrorRecord {
|
||||
promise_id: record.promise_id,
|
||||
arg: -1,
|
||||
error_code: err.kind as i32,
|
||||
error_len: err.kind_str.len() as i32,
|
||||
error_code: err.kind_str.as_bytes().to_owned(),
|
||||
error_message: err.msg.as_bytes().to_owned(),
|
||||
};
|
||||
error_record.into()
|
||||
|
@ -165,7 +171,8 @@ where
|
|||
let error_record = ErrorRecord {
|
||||
promise_id: record.promise_id,
|
||||
arg: -1,
|
||||
error_code: err.kind as i32,
|
||||
error_len: err.kind_str.len() as i32,
|
||||
error_code: err.kind_str.as_bytes().to_owned(),
|
||||
error_message: err.msg.as_bytes().to_owned(),
|
||||
};
|
||||
error_record.into()
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
//! https://url.spec.whatwg.org/#idna
|
||||
|
||||
use super::dispatch_json::{Deserialize, JsonOp, Value};
|
||||
use crate::op_error::{ErrorKind, OpError};
|
||||
use crate::op_error::OpError;
|
||||
use crate::state::State;
|
||||
use deno_core::CoreIsolate;
|
||||
use deno_core::ZeroCopyBuf;
|
||||
|
@ -13,13 +13,6 @@ pub fn init(i: &mut CoreIsolate, s: &State) {
|
|||
i.register_op("op_domain_to_ascii", s.stateful_json_op(op_domain_to_ascii));
|
||||
}
|
||||
|
||||
fn invalid_domain_error() -> OpError {
|
||||
OpError {
|
||||
kind: ErrorKind::TypeError,
|
||||
msg: "Invalid domain.".to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
struct DomainToAscii {
|
||||
|
@ -35,9 +28,10 @@ fn op_domain_to_ascii(
|
|||
let args: DomainToAscii = serde_json::from_value(args)?;
|
||||
let domain = if args.be_strict {
|
||||
domain_to_ascii_strict(args.domain.as_str())
|
||||
.map_err(|_| invalid_domain_error())?
|
||||
.map_err(|_| OpError::invalid_domain_error())?
|
||||
} else {
|
||||
domain_to_ascii(args.domain.as_str()).map_err(|_| invalid_domain_error())?
|
||||
domain_to_ascii(args.domain.as_str())
|
||||
.map_err(|_| OpError::invalid_domain_error())?
|
||||
};
|
||||
Ok(JsonOp::Sync(json!(domain)))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue