Skip to content

Commit

Permalink
refactor: Implement hex arch for credentials & fix error handling
Browse files Browse the repository at this point in the history
Refs: #9 #15 #16
  • Loading branch information
maikbasel committed May 22, 2024
1 parent 5422e6b commit 7d98d63
Show file tree
Hide file tree
Showing 52 changed files with 802 additions and 141 deletions.
35 changes: 35 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"@radix-ui/react-select": "^2.0.0",
"@radix-ui/react-separator": "^1.0.3",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@radix-ui/react-tooltip": "^1.0.7",
"@tanstack/react-table": "^8.11.6",
"@tauri-apps/api": "^1.5.3",
Expand Down
49 changes: 46 additions & 3 deletions src-tauri/src/credentials/core/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use std::fmt::{Display, Formatter};

use error_stack::{Context, Report};
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use serde_json::json;

#[derive(Debug, Eq, PartialEq, Clone, serde::Serialize)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum CredentialsError {
InvalidCredentialsError,
UnexpectedError(String),
Expand All @@ -12,8 +15,8 @@ impl Display for CredentialsError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
CredentialsError::InvalidCredentialsError => write!(f, "invalid credentials error"),
CredentialsError::UnexpectedError(error_code) => {
write!(f, "unexpected error: {}", error_code)
CredentialsError::UnexpectedError(reason) => {
write!(f, "unexpected error: {}", reason)
}
}
}
Expand All @@ -28,9 +31,28 @@ impl From<Report<CredentialsError>> for CredentialsError {
}
}

impl Serialize for CredentialsError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("CredentialsError", 1)?;
let (code, message) = match self {
CredentialsError::InvalidCredentialsError => (
"InvalidCredentialsError",
CredentialsError::InvalidCredentialsError.to_string(),
),
CredentialsError::UnexpectedError(reason) => ("UnexpectedError", reason.to_string()),
};
state.serialize_field("error", &json!({ "code": code, "message": message }))?;
state.end()
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn should_return_invalid_credentials_error_when_calling_from_on_report_with_context_invalid_credentials_error(
Expand All @@ -52,4 +74,25 @@ mod tests {

assert_eq!(error, result);
}

#[test]
fn should_serialize_invalid_credentials_error_to_json() {
let error = CredentialsError::InvalidCredentialsError;
let expected = json!({ "error": {"code": "InvalidCredentialsError", "message": CredentialsError::InvalidCredentialsError.to_string(),} }).to_string();

let serialized = serde_json::to_string(&error).unwrap();

assert_eq!(serialized, expected);
}

#[test]
fn should_serialize_unexpected_error_to_json() {
let error = CredentialsError::UnexpectedError("UnknownError".to_string());
let expected =
json!({ "error": {"code": "UnexpectedError", "message": "UnknownError",} }).to_string();

let serialized = serde_json::to_string(&error).unwrap();

assert_eq!(serialized, expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl CredentialsDataSPI for STSAdapter {
Err(Report::new(CredentialsError::InvalidCredentialsError))
}
_ => {
let error_code = error_code.unwrap_or("UnknownError");
let error_code = error_code.unwrap_or("Server Error");
Err(Report::new(CredentialsError::UnexpectedError(
error_code.to_string(),
)))
Expand Down
77 changes: 76 additions & 1 deletion src-tauri/src/profile/core/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use error_stack::{Context, Report};
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use serde_json::json;
use std::fmt::{Debug, Display, Formatter};

#[derive(Debug, Eq, PartialEq, Clone, serde::Serialize)]
#[derive(Debug, Eq, PartialEq, Clone)]
pub enum ProfileError {
InvalidProfileNameError,
ProfileDataLoadError,
Expand Down Expand Up @@ -37,9 +40,51 @@ impl From<Report<ProfileError>> for ProfileError {
}
}

impl Serialize for ProfileError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut state = serializer.serialize_struct("ProfileError", 1)?;
let (code, message) = match self {
ProfileError::InvalidProfileNameError => (
"InvalidProfileNameError",
ProfileError::InvalidProfileNameError.to_string(),
),
ProfileError::ProfileDataLoadError => (
"ProfileDataLoadError",
ProfileError::ProfileDataLoadError.to_string(),
),
ProfileError::ProfileNotFoundError => (
"ProfileNotFoundError",
ProfileError::ProfileNotFoundError.to_string(),
),
ProfileError::ConfigFileLoadError => (
"ConfigFileLoadError",
ProfileError::ConfigFileLoadError.to_string(),
),
ProfileError::ConfigFileWriteError => (
"ConfigFileWriteError",
ProfileError::ConfigFileWriteError.to_string(),
),
ProfileError::CredentialsFileLoadError => (
"CredentialsFileLoadError",
ProfileError::CredentialsFileLoadError.to_string(),
),
ProfileError::CredentialsFileWriteError => (
"CredentialsFileWriteError",
ProfileError::CredentialsFileWriteError.to_string(),
),
};
state.serialize_field("error", &json!({ "code": code, "message": message }))?;
state.end()
}
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn should_return_profile_data_load_error_when_calling_from_on_report_with_context_profile_data_load_error(
Expand All @@ -62,4 +107,34 @@ mod tests {

assert_eq!(error, result);
}

#[test]
fn serialize_invalid_profile_name_error_to_json() {
let error = ProfileError::InvalidProfileNameError;
let expected = json!({ "error": {"code": "InvalidProfileNameError", "message": ProfileError::InvalidProfileNameError.to_string(),} }).to_string();

let serialized = serde_json::to_string(&error).unwrap();

assert_eq!(serialized, expected);
}

#[test]
fn serialize_profile_data_load_error_to_json() {
let error = ProfileError::ProfileDataLoadError;
let expected = json!({ "error": {"code": "ProfileDataLoadError", "message": ProfileError::ProfileDataLoadError.to_string(),} }).to_string();

let serialized = serde_json::to_string(&error).unwrap();

assert_eq!(serialized, expected);
}

#[test]
fn serialize_profile_not_found_error_to_json() {
let error = ProfileError::ProfileNotFoundError;
let expected = json!({ "error": {"code": "ProfileNotFoundError", "message": ProfileError::ProfileNotFoundError.to_string(),} }).to_string();

let serialized = serde_json::to_string(&error).unwrap();

assert_eq!(serialized, expected);
}
}
5 changes: 3 additions & 2 deletions src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"build": {
"beforeBuildCommand": "npm run build && npm run export",
"beforeBuildCommand": "npm run build",
"beforeDevCommand": "npm run dev",
"devPath": "http://localhost:3000",
"distDir": "../out"
Expand Down Expand Up @@ -50,7 +50,8 @@
"csp": null
},
"updater": {
"active": false
"active": false,
"pubkey": "dW50cnVzdGVkIGNvbW1lbnQ6IG1pbmlzaWduIHB1YmxpYyBrZXk6IEIyNjk0ODAyRkVFODIxNzgKUldSNEllaitBa2hwc24vV3l2Q0toazRBNS91MThzREJzeWxlV3lvR0YySTRLdlliSXNtTXM4VTcK"
},
"windows": [
{
Expand Down
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Header from '@/sections/dashboard/components/header/header';
import { ThemeProvider } from '@/sections/dashboard/components/header/theme-provider';
import { TooltipProvider } from '@/components/ui/tooltip';
import { DIContextProvider } from '@/context/di-context';
import { Toaster } from '@/components/ui/toaster';

export const metadata: Metadata = {
title: 'Create Next App',
Expand Down Expand Up @@ -35,6 +36,7 @@ export default function RootLayout({ children }: Readonly<RootLayoutProps>) {
{children}
</main>
</div>
<Toaster />
</DIContextProvider>
</TooltipProvider>
</ThemeProvider>
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/alert-dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as React from 'react';
import * as AlertDialogPrimitive from '@radix-ui/react-alert-dialog';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';
import { buttonVariants } from '@/components/ui/button';

const AlertDialog = AlertDialogPrimitive.Root;
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import * as React from 'react';
import * as AvatarPrimitive from '@radix-ui/react-avatar';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const Avatar = React.forwardRef<
React.ElementRef<typeof AvatarPrimitive.Root>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/badge.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { cva, type VariantProps } from 'class-variance-authority';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const badgeVariants = cva(
'inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-semibold transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2',
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/breadcrumb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { ChevronRight, MoreHorizontal } from 'lucide-react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const Breadcrumb = React.forwardRef<
HTMLElement,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as React from 'react';
import { Slot } from '@radix-ui/react-slot';
import { cva, type VariantProps } from 'class-variance-authority';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const buttonVariants = cva(
'inline-flex items-center justify-center whitespace-nowrap rounded-md text-sm font-medium ring-offset-background transition-colors focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2 disabled:pointer-events-none disabled:opacity-50',
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/checkbox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import * as CheckboxPrimitive from '@radix-ui/react-checkbox';
import { Check } from 'lucide-react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const Checkbox = React.forwardRef<
React.ElementRef<typeof CheckboxPrimitive.Root>,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/command.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { type DialogProps } from '@radix-ui/react-dialog';
import { Command as CommandPrimitive } from 'cmdk';
import { Search } from 'lucide-react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';
import { Dialog, DialogContent } from '@/components/ui/dialog';

const Command = React.forwardRef<
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/data-table-column-header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@radix-ui/react-icons';
import { Column } from '@tanstack/react-table';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';
import {
DropdownMenu,
DropdownMenuContent,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/data-table-faceted-filter.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react';
import { CheckIcon, PlusCircledIcon } from '@radix-ui/react-icons';
import { Column } from '@tanstack/react-table';
import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';
import { Separator } from '@/components/ui/separator';
import { Button } from '@/components/ui/button';
import {
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import * as DialogPrimitive from '@radix-ui/react-dialog';
import { X } from 'lucide-react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const Dialog = DialogPrimitive.Root;

Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/dropdown-menu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as React from 'react';
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu';
import { Check, ChevronRight, Circle } from 'lucide-react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

const DropdownMenu = DropdownMenuPrimitive.Root;

Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useFormContext,
} from 'react-hook-form';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';
import { Label } from '@/components/ui/label';

const Form = FormProvider;
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/input.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as React from 'react';

import { cn } from '@/lib/css-utils';
import { cn } from '@/lib/utils';

export interface InputProps
extends React.InputHTMLAttributes<HTMLInputElement> {}
Expand Down
Loading

0 comments on commit 7d98d63

Please sign in to comment.