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

[#63] 오류 처리 리팩토링 #83

Merged
merged 19 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ cargo run --bin rrdb run
#### Client

```
psql -U rrdb -p 55555
psql -U rrdb -p 55555 --host 0.0.0.0
```

---
Expand Down
2 changes: 0 additions & 2 deletions src/ast/types/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::convert::TryFrom;

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Clone, Debug, PartialEq, Eq)]
Expand Down
21 changes: 7 additions & 14 deletions src/errors/execute_error.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
use std::error::Error;
use std::string::ToString;
use super::RRDBError;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct ExecuteError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl ExecuteError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
pub fn new<T: ToString>(message: T) -> RRDBError {
RRDBError::ExecuteError(Self {
message: message.to_string(),
}
}

pub fn boxed<T: ToString>(message: T) -> Box<Self> {
Box::new(Self::new(message))
}

pub fn dyn_boxed<T: ToString>(message: T) -> Box<dyn Error + Send> {
Box::new(Self::new(message))
backtrace: std::backtrace::Backtrace::capture(),
})
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/errors/into_error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::string::ToString;
use super::RRDBError;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct IntoError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl IntoError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
pub fn new<T: ToString>(message: T) -> RRDBError {
RRDBError::IntoError(Self {
message: message.to_string(),
}
}

pub fn boxed<T: ToString>(message: T) -> Box<Self> {
Box::new(Self::new(message))
backtrace: std::backtrace::Backtrace::capture(),
})
}
}

Expand Down
16 changes: 7 additions & 9 deletions src/errors/lexing_error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::string::ToString;
use super::RRDBError;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct LexingError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl LexingError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
pub fn new<T: ToString>(message: T) -> RRDBError {
RRDBError::LexingError(Self {
message: message.to_string(),
}
}

pub fn boxed<T: ToString>(message: T) -> Box<Self> {
Box::new(Self::new(message))
backtrace: std::backtrace::Backtrace::capture(),
})
}
}

Expand Down
23 changes: 23 additions & 0 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,26 @@ pub mod parsing_error;
pub mod predule;
pub mod server_error;
pub mod type_error;

#[derive(Debug)]
pub enum RRDBError {
ExecuteError(execute_error::ExecuteError),
IntoError(into_error::IntoError),
LexingError(lexing_error::LexingError),
ParsingError(parsing_error::ParsingError),
ServerError(server_error::ServerError),
TypeError(type_error::TypeError),
}

impl ToString for RRDBError {
fn to_string(&self) -> String {
match self {
RRDBError::ExecuteError(e) => e.to_string(),
RRDBError::IntoError(e) => e.to_string(),
RRDBError::LexingError(e) => e.to_string(),
RRDBError::ParsingError(e) => e.to_string(),
RRDBError::ServerError(e) => e.to_string(),
RRDBError::TypeError(e) => e.to_string(),
}
}
}
16 changes: 7 additions & 9 deletions src/errors/parsing_error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
use std::string::ToString;
use super::RRDBError;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct ParsingError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl ParsingError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
pub fn new<T: ToString>(message: T) -> RRDBError {
RRDBError::ParsingError(Self {
message: message.to_string(),
}
}

pub fn boxed<T: ToString>(message: T) -> Box<Self> {
Box::new(Self::new(message))
backtrace: std::backtrace::Backtrace::capture(),
})
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/errors/server_error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::string::ToString;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct ServerError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl ServerError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
message: message.to_string(),
backtrace: std::backtrace::Backtrace::capture(),
}
}

Expand Down
20 changes: 7 additions & 13 deletions src/errors/type_error.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
use std::{error::Error, string::ToString};
use super::RRDBError;

#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Debug)]
pub struct TypeError {
pub message: String,
pub backtrace: std::backtrace::Backtrace,
}

impl TypeError {
pub fn new<T: ToString>(message: T) -> Self {
Self {
pub fn new<T: ToString>(message: T) -> RRDBError {
RRDBError::TypeError(Self {
message: message.to_string(),
}
}

pub fn boxed<T: ToString>(message: T) -> Box<Self> {
Box::new(Self::new(message))
}

pub fn dyn_boxed<T: ToString>(message: T) -> Box<dyn Error + Send> {
Box::new(Self::new(message))
backtrace: std::backtrace::Backtrace::capture(),
})
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/executor/common.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use std::error::Error;
use std::io::ErrorKind;

use super::config::table::TableConfig;
use super::encoder::storage::StorageEncoder;
use super::predule::Executor;
use crate::ast::types::TableName;
use crate::errors::execute_error::ExecuteError;
use crate::errors::RRDBError;

impl Executor {
pub async fn get_table_config(
&self,
table_name: TableName,
) -> Result<TableConfig, Box<dyn Error + Send>> {
pub async fn get_table_config(&self, table_name: TableName) -> Result<TableConfig, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
Expand All @@ -35,12 +32,12 @@ impl Executor {

match table_config {
Some(table_config) => Ok(table_config),
None => Err(ExecuteError::boxed("invalid config data")),
None => Err(ExecuteError::new("invalid config data")),
}
}
Err(error) => match error.kind() {
ErrorKind::NotFound => Err(ExecuteError::boxed("table not found")),
_ => Err(ExecuteError::boxed(format!("{:?}", error))),
ErrorKind::NotFound => Err(ExecuteError::new("table not found")),
_ => Err(ExecuteError::new(format!("{:?}", error))),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/executor/config/table.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, iter::FromIterator};
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

Expand Down
13 changes: 6 additions & 7 deletions src/executor/executor.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::error::Error;

use crate::ast::ddl::create_database::CreateDatabaseQuery;
use crate::ast::{DDLStatement, DMLStatement, OtherStatement, SQLStatement};
use crate::errors::execute_error::ExecuteError;
use crate::errors::RRDBError;
use crate::executor::predule::ExecuteResult;
use crate::logger::predule::Logger;
use crate::utils::path::get_target_basepath;
Expand All @@ -23,7 +22,7 @@ impl Executor {
}

// 기본 설정파일 세팅
pub async fn init(&self) -> Result<(), Box<dyn Error + Send>> {
pub async fn init(&self) -> Result<(), RRDBError> {
// 루트 디렉터리 생성 (없다면)
let base_path = get_target_basepath();
if let Err(error) = tokio::fs::create_dir(base_path.clone()).await {
Expand All @@ -32,7 +31,7 @@ impl Executor {
} else {
println!("path {:?}", base_path.clone());
println!("error: {:?}", error.to_string());
return Err(ExecuteError::boxed(error.to_string()));
return Err(ExecuteError::new(error.to_string()));
}
}

Expand All @@ -43,7 +42,7 @@ impl Executor {
let global_config = toml::to_string(&global_info).unwrap();

if let Err(error) = tokio::fs::write(global_path, global_config.as_bytes()).await {
return Err(ExecuteError::boxed(error.to_string()));
return Err(ExecuteError::new(error.to_string()));
}

self.create_database(CreateDatabaseQuery::builder().set_name("rrdb".into()))
Expand All @@ -57,7 +56,7 @@ impl Executor {
&self,
statement: SQLStatement,
_connection_id: String,
) -> Result<ExecuteResult, Box<dyn Error + Send>> {
) -> Result<ExecuteResult, RRDBError> {
Logger::info(format!("AST echo: {:?}", statement));

// 쿼리 실행
Expand Down Expand Up @@ -95,7 +94,7 @@ impl Executor {

match result {
Ok(result) => Ok(result),
Err(error) => Err(ExecuteError::boxed(error.to_string())),
Err(error) => Err(ExecuteError::new(error.to_string())),
}
}
}
18 changes: 9 additions & 9 deletions src/executor/implements/ddl/alter_database.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use std::error::Error;
use std::io::ErrorKind;

use crate::ast::ddl::alter_database::{AlterDatabaseAction, AlterDatabaseQuery};
use crate::errors::predule::ExecuteError;
use crate::errors::RRDBError;
use crate::executor::config::database::DatabaseConfig;
use crate::executor::encoder::storage::StorageEncoder;
use crate::executor::predule::{ExecuteResult, Executor};
Expand All @@ -12,7 +12,7 @@ impl Executor {
pub async fn alter_database(
&self,
query: AlterDatabaseQuery,
) -> Result<ExecuteResult, Box<dyn Error + Send>> {
) -> Result<ExecuteResult, RRDBError> {
let encoder = StorageEncoder::new();

let base_path = self.get_base_path();
Expand All @@ -25,7 +25,7 @@ impl Executor {
let from_database_name = query
.database_name
.clone()
.ok_or_else(|| ExecuteError::dyn_boxed("no database name"))?;
.ok_or_else(|| ExecuteError::new("no database name"))?;

// 변경할 데이터베이스명
let to_database_name = rename.name;
Expand All @@ -43,10 +43,10 @@ impl Executor {
if let Err(error) = result {
match error.kind() {
ErrorKind::NotFound => {
return Err(ExecuteError::boxed("database not found"))
return Err(ExecuteError::new("database not found"))
}
_ => {
return Err(ExecuteError::boxed("database alter failed"));
return Err(ExecuteError::new("database alter failed"));
}
}
}
Expand All @@ -69,20 +69,20 @@ impl Executor {
)
.await
{
return Err(ExecuteError::dyn_boxed("no database name"));
return Err(ExecuteError::new("no database name"));
}
}
None => {
return Err(ExecuteError::boxed("invalid config data"));
return Err(ExecuteError::new("invalid config data"));
}
}
}
Err(error) => match error.kind() {
ErrorKind::NotFound => {
return Err(ExecuteError::boxed("database not found"));
return Err(ExecuteError::new("database not found"));
}
_ => {
return Err(ExecuteError::boxed(format!("{:?}", error)));
return Err(ExecuteError::new(format!("{:?}", error)));
}
},
}
Expand Down
Loading
Loading