fix handling error codes

Seto Elkahfi committed Aug 6, 2024 at 13:58 UTC 2957744e18dd2a539bf3c8ae5afa9a44454b1052
12 files changed +62 -37
.bazelrc
+4 -4
@@ -2,10 +2,10 @@
2 common --enable_bzlmod
3
4 # BuildBuddy
5 -build --bes_results_url=https://app.buildbuddy.io/invocation/
6 -build --bes_backend=grpcs://remote.buildbuddy.io
7 -build --nolegacy_important_outputs
8 -try-import ./auth.bazelrc
5 +#build --bes_results_url=https://app.buildbuddy.io/invocation/
6 +#build --bes_backend=grpcs://remote.buildbuddy.io
7 +#build --nolegacy_important_outputs
8 +#try-import ./auth.bazelrc
9
10 ###############################
11 # Filesystem interactions #
backend/musik88-web/app/controllers/api/v1/user_controller.rb
+1 -1
@@ -18,7 +18,7 @@ module Api
18 render_success
19 else
20 logger.debug 'Invalid credentials.'
21 - render_error(GemErrorCodes.invalid_credentials, 'Invalid credentials.', :unauthorized)
21 + render_error(GemErrorCodes.invalid_credentials, GemErrorCodes.invalid_credentials_message, :unauthorized)
22 end
23 end
24
frontend/splitfire-desktop/app/layout.tsx
+1 -7
@@ -3,7 +3,7 @@
3 import { AddressBar } from "../components/ui/address-bar";
4 import { GlobalNav } from "../components/ui/global-nav";
5 import "./globals.css";
6 -import { Suspense, useEffect, useState } from "react";
6 +import { useEffect, useState } from "react";
7 import { CurrentUser, CurrentUserType, db } from "../lib/db";
8 import { UserContext } from "../lib/current-user-context";
9 import { useLogger } from "../lib/logger";
@@ -52,7 +52,6 @@ export default function RootLayout({
52 return (
53 <html lang="en" className="[color-scheme:dark]">
54 <body className="bg-gray-1100 overflow-y-scroll bg-[url('/grid.svg')] pb-36">
55 - Loading app ...
55 </body>
56 </html>
57 );
@@ -62,7 +61,6 @@ export default function RootLayout({
61 return (
62 <html lang="en" className="[color-scheme:dark]">
63 <body className="bg-gray-1100 overflow-y-scroll bg-[url('/grid.svg')] pb-36">
65 - Error loading app ...
64 </body>
65 </html>
66 );
@@ -71,8 +69,6 @@ export default function RootLayout({
69 return (
70 <html lang="en" className="[color-scheme:dark]">
71 <body className="bg-gray-1100 overflow-none bg-[url('/grid.svg')] pb-36">
74 - {state === State.LOADED && user && (
75 - <Suspense>
72 <UserContext.Provider value={{ user, updateUser }}>
73 <GlobalNav />
74 <div className="lg:pl-72">
@@ -90,8 +86,6 @@ export default function RootLayout({
86 </div>
87 </div>
88 </UserContext.Provider>
93 - </Suspense>
94 - )}
89 </body>
90 </html>
91 );
frontend/splitfire-desktop/src-tauri/Cargo.lock
+1
@@ -659,6 +659,7 @@ name = "crate_error_codes"
659 version = "0.1.0"
660 dependencies = [
661 "serde",
662 + "serde_repr",
663 "ts-rs",
664 ]
665
frontend/splitfire-desktop/src-tauri/src/models/account.rs
+2 -2
@@ -1,4 +1,4 @@
1 -use crate_error_codes::UserError;
1 +use crate_error_codes::ErrorCode;
2 use serde::{Deserialize, Serialize};
3 use super::player::TauriResponse;
4
@@ -14,7 +14,7 @@ pub struct AccountLoginResponse {
14 #[derive(Serialize, Deserialize)]
15 #[derive(Debug)]
16 pub struct ErrorResponse {
17 - pub error_code: UserError,
17 + pub error_code: ErrorCode,
18 pub message: String,
19 }
20
frontend/splitfire-desktop/src-tauri/src/rest/account.rs
+11 -11
@@ -12,7 +12,7 @@ use crate::{
12 },
13 rest::parse_error_response,
14 };
15 -use crate_error_codes::UserError;
15 +use crate_error_codes::ErrorCode;
16 use log::{debug, error};
17 use reqwest::Client;
18 use serde_json::json;
@@ -41,10 +41,10 @@ pub async fn account_login(
41 let response = match result {
42 Ok(ok_response) => ok_response,
43 Err(e) => {
44 - debug!("Failed to get response: {:?}", e);
44 + error!("Failed to get response: {:?}", e);
45 return Err(ErrorResponse {
46 - error_code: UserError::NetworkError,
47 - message: UserError::NetworkError.error_message().to_string(),
46 + error_code: ErrorCode::NetworkError,
47 + message: ErrorCode::NetworkError.error_message().to_string(),
48 });
49 }
50 };
@@ -52,7 +52,7 @@ pub async fn account_login(
52 let ok_response = match response.status() {
53 reqwest::StatusCode::OK => response,
54 _ => {
55 - debug!("Failed to login: {:?}", response);
55 + error!("Failed to login: {:?}", response);
56 let error_response = parse_error_response(response).await;
57 return error_response;
58 }
@@ -66,8 +66,8 @@ pub async fn account_login(
66 Err(e) => {
67 error!("Cannot convert token to str: {:?}", e);
68 return Err(ErrorResponse {
69 - error_code: UserError::ParseError,
70 - message: UserError::ParseError.error_message().to_string(),
69 + error_code: ErrorCode::ParseError,
70 + message: ErrorCode::ParseError.error_message().to_string(),
71 });
72 }
73 };
@@ -76,8 +76,8 @@ pub async fn account_login(
76 None => {
77 error!("No Authorization header found.");
78 return Err(ErrorResponse {
79 - error_code: UserError::ParseError,
80 - message: UserError::ParseError.error_message().to_string(),
79 + error_code: ErrorCode::ParseError,
80 + message: ErrorCode::ParseError.error_message().to_string(),
81 });
82 }
83 };
@@ -87,8 +87,8 @@ pub async fn account_login(
87 Err(e) => {
88 error!("Failed to parse response: {:?}", e);
89 return Err(ErrorResponse {
90 - error_code: UserError::ParseError,
91 - message: UserError::ParseError.error_message().to_string(),
90 + error_code: ErrorCode::ParseError,
91 + message: ErrorCode::ParseError.error_message().to_string(),
92 });
93 }
94 };
frontend/splitfire-desktop/src-tauri/src/rest/mod.rs
+7 -2
@@ -1,5 +1,5 @@
1 use crate::models::account::ErrorResponse;
2 -use crate_error_codes::UserError;
2 +use crate_error_codes::ErrorCode;
3 use log::{debug, error};
4 use reqwest::Response;
5
@@ -10,9 +10,14 @@ async fn parse_error_response<T>(response: Response) -> Result<T, ErrorResponse>
10 let e: ErrorResponse = match response.json().await {
11 Ok(json) => json,
12 Err(e) => {
13 + //debug!("Received text: {:?}", response.text().await);
14 + //return Err(ErrorResponse {
15 + // error_code: ErrorCode::InvalidRequest,
16 + // message: "test".to_string()
17 + //});
18 error!("Failed to parse error response: {:?}", e);
19 return Err(ErrorResponse {
15 - error_code: UserError::ParseError,
20 + error_code: ErrorCode::ParseError,
21 message: e.to_string(),
22 });
23 }
lib/crate_error_codes/Cargo.lock
+12
@@ -7,6 +7,7 @@ name = "crate_error_codes"
7 version = "0.1.0"
8 dependencies = [
9 "serde",
10 + "serde_repr",
11 "ts-rs",
12 ]
13
@@ -48,6 +49,17 @@ dependencies = [
49 "syn",
50 ]
51
52 +[[package]]
53 +name = "serde_repr"
54 +version = "0.1.19"
55 +source = "registry+https://github.com/rust-lang/crates.io-index"
56 +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9"
57 +dependencies = [
58 + "proc-macro2",
59 + "quote",
60 + "syn",
61 +]
62 +
63 [[package]]
64 name = "syn"
65 version = "2.0.72"
lib/crate_error_codes/Cargo.toml
+1
@@ -7,4 +7,5 @@ edition = "2021"
7
8 [dependencies]
9 serde = { version = "1.0", features = ["derive"] }
10 +serde_repr = "0.1"
11 ts-rs = "9.0"
\ No newline at end of file
lib/crate_error_codes/src/lib.rs
+9 -9
@@ -1,12 +1,12 @@
1 -use serde::{Deserialize, Serialize};
1 +use serde_repr::{Deserialize_repr, Serialize_repr};
2 use ts_rs::TS;
3
4 #[derive(TS)]
5 #[ts(export)]
6 +#[derive(Serialize_repr, Deserialize_repr)]
7 #[repr(i32)]
7 -#[derive(Serialize, Deserialize)]
8 #[derive(Debug)]
9 -pub enum UserError {
9 +pub enum ErrorCode {
10 // User defined error codes starts from 1000
11 UserNotFound = 1000,
12 InvalidCredentials = 1001,
@@ -17,14 +17,14 @@ pub enum UserError {
17 NetworkError = 3,
18 }
19
20 -impl UserError {
20 +impl ErrorCode {
21 pub fn error_message(&self) -> &str {
22 match self {
23 - UserError::UserNotFound => "User not found.",
24 - UserError::InvalidRequest => "Invalid request.",
25 - UserError::ParseError => "Failed to parse response.",
26 - UserError::NetworkError => "Failed to get response.",
27 - UserError::InvalidCredentials => "Invalid credentials.",
23 + ErrorCode::UserNotFound => "User not found.",
24 + ErrorCode::InvalidRequest => "Invalid request.",
25 + ErrorCode::ParseError => "Failed to parse response.",
26 + ErrorCode::NetworkError => "Failed to get response.",
27 + ErrorCode::InvalidCredentials => "Invalid credentials.",
28 }
29 }
30
lib/gem_error_codes/Cargo.lock
+12
@@ -68,6 +68,7 @@ name = "crate_error_codes"
68 version = "0.1.0"
69 dependencies = [
70 "serde",
71 + "serde_repr",
72 "ts-rs",
73 ]
74
@@ -282,6 +283,17 @@ dependencies = [
283 "syn",
284 ]
285
286 +[[package]]
287 +name = "serde_repr"
288 +version = "0.1.19"
289 +source = "registry+https://github.com/rust-lang/crates.io-index"
290 +checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9"
291 +dependencies = [
292 + "proc-macro2",
293 + "quote",
294 + "syn",
295 +]
296 +
297 [[package]]
298 name = "shell-words"
299 version = "1.1.0"
lib/gem_error_codes/ext/gem_error_codes/src/lib.rs
+1 -1
@@ -1,4 +1,4 @@
1 -use crate_error_codes::UserError::{InvalidCredentials, UserNotFound};
1 +use crate_error_codes::ErrorCode::{InvalidCredentials, UserNotFound};
2 use magnus::{function, prelude::*, Error, Ruby};
3
4 fn user_not_found() -> i32 {