Skip to content

Commit

Permalink
Improved messages for json errors (pgcentralfoundation#1893)
Browse files Browse the repository at this point in the history
  • Loading branch information
YohDeadfall authored Oct 9, 2024
1 parent 45cac5c commit 24c4600
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions pgrx/src/datum/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ impl FromDatum for Json {
let len = varsize_any_exhdr(varlena);
let data = vardata_any(varlena);
let slice = std::slice::from_raw_parts(data as *const u8, len);
let value = serde_json::from_slice(slice).expect("failed to parse Json value");
let value =
serde_json::from_slice(slice).expect("datum must refer to a valid json varlena");
Some(Json(value))
}
}
Expand All @@ -67,12 +68,12 @@ impl FromDatum for JsonB {
pg_sys::jsonb_out,
&[Some(detoasted.into())],
)
.expect("failed to convert jsonb to a cstring");
.expect("datum must refer to a valid jsonb varlena");

let value = serde_json::from_str(
cstr.to_str().expect("text version of jsonb is not valid UTF8"),
cstr.to_str().expect("a text version of the jsonb must be valid utf-8"),
)
.expect("failed to parse JsonB value");
.expect("a text version of jsonb must be a valid json");

// free the cstring returned from direct_function_call -- we don't need it anymore
pg_sys::pfree(cstr.as_ptr() as void_mut_ptr);
Expand Down Expand Up @@ -122,7 +123,7 @@ impl FromDatum for JsonString {
/// for json
impl IntoDatum for Json {
fn into_datum(self) -> Option<pg_sys::Datum> {
let string = serde_json::to_string(&self.0).expect("failed to serialize Json value");
let string = serde_json::to_string(&self.0).unwrap();
string.into_datum()
}

Expand All @@ -134,9 +135,9 @@ impl IntoDatum for Json {
/// for jsonb
impl IntoDatum for JsonB {
fn into_datum(self) -> Option<pg_sys::Datum> {
let string = serde_json::to_string(&self.0).expect("failed to serialize JsonB value");
let cstring =
alloc::ffi::CString::new(string).expect("string version of jsonb is not valid UTF8");
let string = serde_json::to_string(&self.0).unwrap();
let cstring = alloc::ffi::CString::new(string)
.expect("a text version of jsonb must contain no null terminator");

unsafe { direct_function_call_as_datum(pg_sys::jsonb_in, &[Some(cstring.as_ptr().into())]) }
}
Expand Down

0 comments on commit 24c4600

Please sign in to comment.