feat: Impl ErrorExt for opaque error and ParseError

This commit is contained in:
evenyag
2022-04-29 16:51:55 +08:00
parent 08ccb466cb
commit 7e2e3e3429
8 changed files with 68 additions and 11 deletions

View File

@@ -6,5 +6,6 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
common-error = { path = "../common/error" }
snafu = { version = "0.7", features = ["backtraces"] }
sqlparser = "0.15.0"

View File

@@ -1,4 +1,5 @@
use snafu::prelude::*;
use common_error::prelude::*;
use snafu::{prelude::*, ErrorCompat};
use sqlparser::parser::ParserError as SpParserError;
/// SQL parser errors.
@@ -29,6 +30,19 @@ pub enum ParserError {
SpSyntax { sql: String, source: SpParserError },
}
impl ErrorExt for ParserError {
fn status_code(&self) -> StatusCode {
match self {
Self::Unsupported { .. } => StatusCode::Unsupported,
Self::Unexpected { .. } | Self::SpSyntax { .. } => StatusCode::InvalidSyntax,
}
}
fn backtrace_opt(&self) -> Option<&Backtrace> {
ErrorCompat::backtrace(self)
}
}
#[cfg(test)]
mod tests {
use std::assert_matches::assert_matches;