Skip to content

Commit 8c12f12

Browse files
committed
More cleanup.
1 parent 91d493c commit 8c12f12

File tree

2 files changed

+37
-42
lines changed

2 files changed

+37
-42
lines changed

src/rust_utils.rs

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ use std::fmt;
55
use std::io;
66
use std::io::{Error,ErrorKind,Result};
77
use std::mem;
8+
use std::ffi::CString;
89

910
use crate::utils::elog::*;
1011
use crate::utils::memutils::*;
1112
use crate::utils::memutils::c::*;
1213
use crate::utils::palloc::*;
1314

15+
#[derive(Clone,Copy)]
1416
pub enum PanicType {
1517
ReThrow,
1618
Errfinish,
@@ -55,8 +57,8 @@ macro_rules! longjmp_panic {
5557
unsafe {
5658
use postgres_extension::utils::elog
5759
::{PG_exception_stack,
58-
error_context_stack,
59-
PanicReThrow};
60+
error_context_stack};
61+
use postgres_extension::rust_utils::PanicType;
6062
use postgres_extension::setjmp::{sigsetjmp,sigjmp_buf};
6163
let save_exception_stack: *mut sigjmp_buf = PG_exception_stack;
6264
let save_context_stack: *mut ErrorContextCallback = error_context_stack;
@@ -67,7 +69,7 @@ macro_rules! longjmp_panic {
6769
} else {
6870
PG_exception_stack = save_exception_stack;
6971
error_context_stack = save_context_stack;
70-
panic!(PanicReThrow);
72+
panic!(PanicType::ReThrow);
7173
}
7274
PG_exception_stack = save_exception_stack;
7375
error_context_stack = save_context_stack;
@@ -94,39 +96,37 @@ pub fn init_error_handling() {
9496
}
9597
}
9698

97-
pub fn handle_panic(panic_payload: Box<dyn std::any::Any>) -> PanicType {
98-
if panic_payload.is::<PanicReThrow>() {
99-
PanicType::ReThrow
100-
} else if panic_payload.is::<PanicErrfinish>() {
101-
PanicType::Errfinish
102-
} else {
103-
use std::ffi::CString;
104-
105-
let panic_message =
106-
if let Some(err_str) = panic_payload.downcast_ref::<&str>() {
107-
format!("{}", err_str)
108-
} else {
109-
format!("{:?}", panic_payload)
110-
};
99+
pub fn handle_panic(payload: Box<dyn std::any::Any>) -> PanicType {
100+
if let Some(panictype) = payload.downcast_ref::<PanicType>() {
101+
return *panictype
102+
}
111103

112-
let message = format!("rust panic: {}", panic_message);
113-
let hint = "find out what rust code caused the panic";
114-
let detail = "some rust code caused a panic";
104+
let panic_message =
105+
if let Some(s) = payload.downcast_ref::<&str>() {
106+
s
107+
} else if let Some(s) = payload.downcast_ref::<String>() {
108+
&s[..]
109+
} else {
110+
"Box<Any>"
111+
};
115112

116-
let cmessage = CString::new(message.as_str()).unwrap();
117-
let chint = CString::new(hint).unwrap();
118-
let cdetail = CString::new(detail).unwrap();
113+
let message = format!("rust panic: {}", panic_message);
114+
let hint = "find out what rust code caused the panic";
115+
let detail = "some rust code caused a panic";
119116

120-
unsafe {
121-
pg_errstart(ERROR, file!(), line!());
122-
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION);
123-
errmsg(cmessage.as_ptr());
124-
errhint(chint.as_ptr());
125-
errdetail(cdetail.as_ptr());
126-
}
117+
let cmessage = CString::new(message.as_str()).unwrap();
118+
let chint = CString::new(hint).unwrap();
119+
let cdetail = CString::new(detail).unwrap();
127120

128-
PanicType::Errfinish
121+
unsafe {
122+
pg_errstart(ERROR, file!(), line!());
123+
errcode(ERRCODE_EXTERNAL_ROUTINE_EXCEPTION);
124+
errmsg(cmessage.as_ptr());
125+
errhint(chint.as_ptr());
126+
errdetail(cdetail.as_ptr());
129127
}
128+
129+
PanicType::Errfinish
130130
}
131131

132132
// implement Write trait for &[i8] (a.k.a. &[c_char])

src/utils/elog.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,19 @@ macro_rules! elog {
2222
macro_rules! ereport {
2323
($elevel:expr, ($($kind:tt($($args:expr),*)),+)) => {
2424
unsafe {
25-
use postgres_extension::utils::elog::{
26-
PanicErrfinish, ERROR,
27-
pg_errstart,errfinish,
28-
errmsg,errhint,errcode,errdetail};
25+
use postgres_extension::utils::elog;
26+
use postgres_extension::rust_utils::PanicType;
2927

30-
if pg_errstart($elevel, file!(), line!()) {
28+
if elog::pg_errstart($elevel, file!(), line!()) {
3129

3230
$(
3331
pg_errfmt!($kind,$($args),+);
3432
)+
3533

36-
if $elevel >= ERROR {
37-
panic!(PanicErrfinish);
34+
if $elevel >= elog::ERROR {
35+
panic!(PanicType::Errfinish);
3836
} else {
39-
errfinish(0);
37+
elog::errfinish(0);
4038
}
4139
}
4240
}
@@ -126,6 +124,3 @@ extern "C" {
126124
pub static mut error_context_stack: *mut ErrorContextCallback;
127125
}
128126

129-
pub struct PanicErrfinish;
130-
pub struct PanicReThrow;
131-

0 commit comments

Comments
 (0)