Skip to content

Commit

Permalink
fix: Error handling over wire
Browse files Browse the repository at this point in the history
  • Loading branch information
can-keklik committed Feb 17, 2024
1 parent bd8d0af commit 78e1af6
Show file tree
Hide file tree
Showing 13 changed files with 54 additions and 35 deletions.
7 changes: 4 additions & 3 deletions cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ impl ClientSession {
}
}

pub async fn handle(&mut self) {
if let Some(message) = self.conn.read().await.unwrap() {
println!("{:?}", message);
pub async fn handle(&mut self) -> Result<Message, ()> {
match self.conn.read().await.unwrap() {
Some(message) => Ok(message),
None => Err(())
}
}

Expand Down
24 changes: 19 additions & 5 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
};

use clap::Parser;
use lykiadb_server::net::{Message, Request};
use lykiadb_server::{net::{Message, Request, Response}, runtime::error::report_error};
use lykiadb_shell::ClientSession;
use tokio::net::TcpStream;

Expand Down Expand Up @@ -35,8 +35,16 @@ async fn run_repl() {
.await
.unwrap();

let response = session.handle().await;
println!("{:?}", response);
let response = session.handle().await.unwrap();

match response {
Message::Response(Response::Value(result))
=> println!("{result}"),
Message::Response(Response::Error(err))
=> report_error("prompt", &line, err.clone()),
_ => panic!("")
}

line.clear();
}
}
Expand All @@ -61,8 +69,14 @@ async fn run_file(filename: &str, print_ast: bool) {

session.send(msg).await.unwrap();

let response = session.handle().await;
println!("{:?}", response);
let response = session.handle().await.unwrap();
match response {
Message::Response(Response::Value(result))
=> println!("{result}"),
Message::Response(Response::Error(err))
=> report_error(filename, &content, err.clone()),
_ => panic!("")
}
}

#[tokio::main]
Expand Down
4 changes: 2 additions & 2 deletions server/src/lang/ast/expr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use serde::Serialize;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::lang::{
Expand Down Expand Up @@ -202,5 +202,5 @@ impl Spanned for Expr {
}

#[repr(transparent)]
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[derive(Debug, Eq, PartialEq, Clone, Copy, Deserialize)]
pub struct ExprId(pub usize);
3 changes: 2 additions & 1 deletion server/src/lang/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ use crate::lang::tokens::token::{
use crate::lang::Literal;
use crate::{kw, skw, sym};
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ParseError {
UnexpectedToken { token: Token },
MissingToken { token: Token, expected: TokenType },
Expand Down
4 changes: 2 additions & 2 deletions server/src/lang/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::sync::Arc;

use rustc_hash::FxHashMap;
use serde::Serialize;
use serde::{Deserialize, Serialize};

use self::ast::expr::ExprId;

pub mod ast;
pub mod tokens;

#[derive(Debug, Clone, PartialEq, Serialize)]
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Str(Arc<String>),
Num(f64),
Expand Down
4 changes: 3 additions & 1 deletion server/src/lang/tokens/scanner.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use serde::{Deserialize, Serialize};

use crate::lang::tokens::token::Symbol::*;
use crate::lang::tokens::token::TokenType::{Eof, Identifier};
use crate::lang::tokens::token::*;
Expand All @@ -12,7 +14,7 @@ pub struct Scanner<'a> {
line: u32,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ScanError {
UnexpectedCharacter { span: Span },
UnterminatedString { span: Span },
Expand Down
15 changes: 7 additions & 8 deletions server/src/lang/tokens/token.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::lang::{Identifier, Literal};
use phf::phf_map;
use serde::Serialize;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Symbol {
Comma,
Colon,
Expand Down Expand Up @@ -30,7 +30,7 @@ pub enum Symbol {
LogicalOr,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum TokenType {
Str,
Num,
Expand All @@ -48,7 +48,7 @@ pub enum TokenType {
Eof,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum Keyword {
Class,
Else,
Expand All @@ -65,7 +65,7 @@ pub enum Keyword {
Loop,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub enum SqlKeyword {
/*
Bool,
Expand Down Expand Up @@ -261,12 +261,11 @@ pub static SQL_KEYWORDS: phf::Map<&'static str, TokenType> = phf_map! {
"WRITE" => skw!(SqlKeyword::Write),
};

#[derive(Debug, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Token {
pub tok_type: TokenType,
pub literal: Option<Literal>,
pub lexeme: Option<String>,
#[serde(skip)]
pub span: Span,
}

Expand All @@ -282,7 +281,7 @@ impl Token {
}
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Span {
pub start: usize,
pub end: usize,
Expand Down
8 changes: 3 additions & 5 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::io::Error;
use tokio::net::{TcpListener, TcpStream};
use tokio_stream::wrappers::TcpListenerStream;
use tokio_stream::StreamExt as _;
use tracing::info;
use tracing::{error, info};

const ASCII_ART: &str = r"
$$\ $$\ $$\ $$$$$$$\ $$$$$$$\
Expand Down Expand Up @@ -65,15 +65,13 @@ impl ServerSession {
let err = execution.err().unwrap();

self.conn
.write(Message::Response(Response::Value(
bson::to_bson(&format!("Error: {:?}", err)).unwrap(),
)))
.write(Message::Response(Response::Error(err)))
.await
.unwrap();
}
}
},
_ => panic!("Unsupported message type"),
_ => error!("Unsupported message type"),
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions server/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use tokio::{
net::TcpStream,
};

use crate::runtime::error::ExecutionError;

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Request {
Run(String),
Expand All @@ -15,6 +17,7 @@ pub enum Request {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Response {
Value(Bson),
Error(ExecutionError)
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand Down
4 changes: 3 additions & 1 deletion server/src/runtime/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use serde::{Deserialize, Serialize};

use super::{interpreter::InterpretError, resolver::ResolveError};
use crate::lang::{ast::parser::ParseError, tokens::scanner::ScanError, tokens::token::Span};

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ExecutionError {
Scan(ScanError),
Parse(ParseError),
Expand Down
3 changes: 2 additions & 1 deletion server/src/runtime/interpreter.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};

use super::environment::{EnvId, Environment};
use super::resolver::Resolver;
Expand All @@ -18,7 +19,7 @@ use crate::util::alloc_shared;
use std::sync::Arc;
use std::vec;

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum InterpretError {
NotCallable {
span: Span,
Expand Down
7 changes: 2 additions & 5 deletions server/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use serde_json::Value;
use tracing::info;

use self::environment::Environment;
use self::error::{report_error, ExecutionError};
use self::error::ExecutionError;
use self::interpreter::{HaltReason, Output};
use self::resolver::Resolver;
use self::std::stdlib;
Expand All @@ -18,7 +18,7 @@ use crate::runtime::types::RV;
use crate::util::Shared;

pub mod environment;
mod error;
pub mod error;
pub mod interpreter;
mod resolver;
mod std;
Expand Down Expand Up @@ -50,13 +50,11 @@ impl Runtime {
let tokens = Scanner::scan(source);
if tokens.is_err() {
let error = error::ExecutionError::Scan(tokens.err().unwrap());
report_error("filename", source, error.clone());
return Err(error);
}
let program = Parser::parse(&tokens.unwrap());
if program.is_err() {
let error = error::ExecutionError::Parse(program.err().unwrap());
report_error("filename", source, error.clone());
return Err(error);
}
let program_unw = program.unwrap();
Expand Down Expand Up @@ -89,7 +87,6 @@ impl Runtime {
HaltReason::Return(rv) => Ok(rv),
HaltReason::Error(interpret_err) => {
let error = error::ExecutionError::Interpret(interpret_err);
report_error("<script>", source, error.clone());
Err(error)
}
}
Expand Down
3 changes: 2 additions & 1 deletion server/src/runtime/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::lang::tokens::token::Span;
use crate::lang::{Identifier, Literal};
use crate::runtime::types::RV;
use rustc_hash::FxHashMap;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

pub struct Resolver {
Expand All @@ -14,7 +15,7 @@ pub struct Resolver {
arena: Arc<AstArena>,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ResolveError {
GenericError { span: Span, message: String },
}
Expand Down

0 comments on commit 78e1af6

Please sign in to comment.