Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved messages for json errors #1893

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading