Skip to content

Commit f9fc1a9

Browse files
committed
chore: extract protocol error to a separate file
1 parent 9641cf3 commit f9fc1a9

File tree

3 files changed

+74
-69
lines changed

3 files changed

+74
-69
lines changed

rust/cubesql/cubesql/src/sql/dataframe.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ use datafusion::arrow::{
1111
datatypes::{DataType, IntervalUnit, Schema, TimeUnit},
1212
record_batch::RecordBatch,
1313
};
14-
// Type aliases for compatibility - actual implementations are in pg-srv
15-
pub type IntervalValue = pg_srv::IntervalValue;
16-
pub type TimestampValue = pg_srv::TimestampValue;
1714
use rust_decimal::prelude::*;
1815
use serde::{Serialize, Serializer};
1916
use std::fmt::Debug;
@@ -78,6 +75,10 @@ impl Row {
7875
}
7976
}
8077

78+
// Type aliases for compatibility - actual implementations are in pg-srv
79+
pub type IntervalValue = pg_srv::IntervalValue;
80+
pub type TimestampValue = pg_srv::TimestampValue;
81+
8182
#[derive(Debug)]
8283
pub enum TableValue {
8384
Null,

rust/cubesql/pg-srv/src/error.rs

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//! Protocol error types for PostgreSQL wire protocol
2+
3+
use crate::protocol;
4+
use std::{backtrace::Backtrace, fmt::Formatter};
5+
6+
/// Protocol error abstract of handled/unhandled errors, it should not handle any kind of business logic errors
7+
/// TODO: Migrate back to thiserror crate, when Rust will stabilize feature(error_generic_member_access)
8+
#[derive(Debug)]
9+
pub enum ProtocolError {
10+
IO {
11+
source: std::io::Error,
12+
backtrace: Backtrace,
13+
},
14+
ErrorResponse {
15+
source: protocol::ErrorResponse,
16+
backtrace: Backtrace,
17+
},
18+
}
19+
20+
impl std::fmt::Display for ProtocolError {
21+
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22+
match self {
23+
ProtocolError::IO { source, .. } => f.write_fmt(format_args!("IO error: {}", source)),
24+
ProtocolError::ErrorResponse { source, .. } => {
25+
f.write_fmt(format_args!("Error: {}", source.message))
26+
}
27+
}
28+
}
29+
}
30+
31+
impl From<std::io::Error> for ProtocolError {
32+
fn from(source: std::io::Error) -> Self {
33+
ProtocolError::IO {
34+
source,
35+
backtrace: Backtrace::capture(),
36+
}
37+
}
38+
}
39+
40+
impl From<protocol::ErrorResponse> for ProtocolError {
41+
fn from(source: protocol::ErrorResponse) -> Self {
42+
ProtocolError::ErrorResponse {
43+
source,
44+
backtrace: Backtrace::capture(),
45+
}
46+
}
47+
}
48+
49+
impl ProtocolError {
50+
/// Return Backtrace from any variant of Enum
51+
pub fn backtrace(&self) -> Option<&Backtrace> {
52+
match &self {
53+
ProtocolError::IO { backtrace, .. } => Some(backtrace),
54+
ProtocolError::ErrorResponse { backtrace, .. } => Some(backtrace),
55+
}
56+
}
57+
58+
/// Converts Error to protocol::ErrorResponse which is usefully for writing response to the client
59+
pub fn to_error_response(self) -> protocol::ErrorResponse {
60+
match self {
61+
ProtocolError::IO { source, .. } => protocol::ErrorResponse::error(
62+
protocol::ErrorCode::InternalError,
63+
source.to_string(),
64+
),
65+
ProtocolError::ErrorResponse { source, .. } => source,
66+
}
67+
}
68+
}

rust/cubesql/pg-srv/src/lib.rs

Lines changed: 2 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod decoding;
66
mod encoding;
77

88
pub mod buffer;
9+
pub mod error;
910
pub mod extended;
1011
pub mod interval;
1112
pub mod pg_type;
@@ -15,73 +16,8 @@ pub mod timestamp;
1516
pub use buffer::*;
1617
pub use decoding::*;
1718
pub use encoding::*;
19+
pub use error::*;
1820
pub use extended::*;
1921
pub use interval::*;
2022
pub use pg_type::*;
2123
pub use timestamp::*;
22-
23-
use std::{backtrace::Backtrace, fmt::Formatter};
24-
25-
/// Protocol error abstract of handled/unhandled errors, it should not handle any kind of business logic errors
26-
/// TODO: Migrate back to thiserror crate, when Rust will stabilize feature(error_generic_member_access)
27-
#[derive(Debug)]
28-
pub enum ProtocolError {
29-
IO {
30-
source: std::io::Error,
31-
backtrace: Backtrace,
32-
},
33-
ErrorResponse {
34-
source: protocol::ErrorResponse,
35-
backtrace: Backtrace,
36-
},
37-
}
38-
39-
impl std::fmt::Display for ProtocolError {
40-
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
41-
match self {
42-
ProtocolError::IO { source, .. } => f.write_fmt(format_args!("IO error: {}", source)),
43-
ProtocolError::ErrorResponse { source, .. } => {
44-
f.write_fmt(format_args!("Error: {}", source.message))
45-
}
46-
}
47-
}
48-
}
49-
50-
impl From<std::io::Error> for ProtocolError {
51-
fn from(source: std::io::Error) -> Self {
52-
ProtocolError::IO {
53-
source,
54-
backtrace: Backtrace::capture(),
55-
}
56-
}
57-
}
58-
59-
impl From<protocol::ErrorResponse> for ProtocolError {
60-
fn from(source: protocol::ErrorResponse) -> Self {
61-
ProtocolError::ErrorResponse {
62-
source,
63-
backtrace: Backtrace::capture(),
64-
}
65-
}
66-
}
67-
68-
impl ProtocolError {
69-
/// Return Backtrace from any variant of Enum
70-
pub fn backtrace(&self) -> Option<&Backtrace> {
71-
match &self {
72-
ProtocolError::IO { backtrace, .. } => Some(backtrace),
73-
ProtocolError::ErrorResponse { backtrace, .. } => Some(backtrace),
74-
}
75-
}
76-
77-
/// Converts Error to protocol::ErrorResponse which is usefully for writing response to the client
78-
pub fn to_error_response(self) -> protocol::ErrorResponse {
79-
match self {
80-
ProtocolError::IO { source, .. } => protocol::ErrorResponse::error(
81-
protocol::ErrorCode::InternalError,
82-
source.to_string(),
83-
),
84-
ProtocolError::ErrorResponse { source, .. } => source,
85-
}
86-
}
87-
}

0 commit comments

Comments
 (0)