-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(glowsquid): 🥅 an unholy error handling solution
At least it's 100% typesafe. Signed-off-by: Suyashtnt <[email protected]>
- Loading branch information
Showing
12 changed files
with
338 additions
and
352 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
|
||
// This file was generated by [tauri-specta](https://github.com/oscartbeaumont/tauri-specta). Do not edit this file manually. | ||
|
||
/** user-defined commands **/ | ||
|
||
export const commands = { | ||
async greet(name: string) : Promise<string> { | ||
return await TAURI_INVOKE("greet", { name }); | ||
}, | ||
async addAccount() : Promise<Result<MinecraftProfile, Error<AuthError>>> { | ||
try { | ||
return { status: "ok", data: await TAURI_INVOKE("add_account") }; | ||
} catch (e) { | ||
if(e instanceof Error) throw e; | ||
else return { status: "error", error: e as any }; | ||
} | ||
} | ||
} | ||
|
||
/** user-defined events **/ | ||
|
||
|
||
|
||
/** user-defined statics **/ | ||
|
||
|
||
|
||
/** user-defined types **/ | ||
|
||
export type AuthError = "MsToken" | "MinecraftToken" | "MinecraftProfile" | ||
export type Cape = { id: string; state: UsageState; url: string; alias: string } | ||
/** | ||
* A custom error type that wraps around a error-stack error, converting it from Report<T> to Error<T> | ||
* | ||
* Also known as the most unholy error type known to crabkind | ||
*/ | ||
export type Error<T> = { error: T; report: JsonValue } | ||
export type JsonValue = null | boolean | number | string | JsonValue[] | { [key in string]: JsonValue } | ||
export type MinecraftProfile = { id: string; name: string; skins: Skin[]; capes: Cape[] } | ||
export type Skin = { id: string; state: UsageState; url: string; variant: SkinVariant } | ||
export type SkinVariant = "CLASSIC" | "SLIM" | ||
export type UsageState = "ACTIVE" | "INACTIVE" | ||
|
||
/** tauri-specta globals **/ | ||
|
||
import { invoke as TAURI_INVOKE } from "@tauri-apps/api/core"; | ||
import * as TAURI_API_EVENT from "@tauri-apps/api/event"; | ||
import { type WebviewWindow as __WebviewWindow__ } from "@tauri-apps/api/webviewWindow"; | ||
|
||
type __EventObj__<T> = { | ||
listen: ( | ||
cb: TAURI_API_EVENT.EventCallback<T> | ||
) => ReturnType<typeof TAURI_API_EVENT.listen<T>>; | ||
once: ( | ||
cb: TAURI_API_EVENT.EventCallback<T> | ||
) => ReturnType<typeof TAURI_API_EVENT.once<T>>; | ||
emit: T extends null | ||
? (payload?: T) => ReturnType<typeof TAURI_API_EVENT.emit> | ||
: (payload: T) => ReturnType<typeof TAURI_API_EVENT.emit>; | ||
}; | ||
|
||
export type Result<T, E> = | ||
| { status: "ok"; data: T } | ||
| { status: "error"; error: E }; | ||
|
||
function __makeEvents__<T extends Record<string, any>>( | ||
mappings: Record<keyof T, string> | ||
) { | ||
return new Proxy( | ||
{} as unknown as { | ||
[K in keyof T]: __EventObj__<T[K]> & { | ||
(handle: __WebviewWindow__): __EventObj__<T[K]>; | ||
}; | ||
}, | ||
{ | ||
get: (_, event) => { | ||
const name = mappings[event as keyof T]; | ||
|
||
return new Proxy((() => {}) as any, { | ||
apply: (_, __, [window]: [__WebviewWindow__]) => ({ | ||
listen: (arg: any) => window.listen(name, arg), | ||
once: (arg: any) => window.once(name, arg), | ||
emit: (arg: any) => window.emit(name, arg), | ||
}), | ||
get: (_, command: keyof __EventObj__<any>) => { | ||
switch (command) { | ||
case "listen": | ||
return (arg: any) => TAURI_API_EVENT.listen(name, arg); | ||
case "once": | ||
return (arg: any) => TAURI_API_EVENT.once(name, arg); | ||
case "emit": | ||
return (arg: any) => TAURI_API_EVENT.emit(name, arg); | ||
} | ||
}, | ||
}); | ||
}, | ||
} | ||
); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
use std::{ | ||
error, | ||
fmt::{self, Debug}, | ||
}; | ||
|
||
use serde::Serialize; | ||
use serde_json::Value; | ||
use specta::Type; | ||
|
||
/// A custom error type that wraps around a error-stack error, converting it from Report<T> to Error<T> | ||
/// | ||
/// Also known as the most unholy error type known to crabkind | ||
#[derive(Debug, Serialize, Type)] | ||
pub struct Error<T> { | ||
error: T, | ||
report: Value, | ||
#[serde(skip)] | ||
error_stack: error_stack::Report<T>, | ||
} | ||
|
||
impl<T: error::Error> error::Error for Error<T> {} | ||
|
||
impl<T> fmt::Display for Error<T> { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
std::fmt::Display::fmt(&self.error_stack, f) | ||
} | ||
} | ||
|
||
// copy instead of clone to enforce cheapness | ||
impl<T: Send + Sync + error::Error + Copy + 'static> From<error_stack::Report<T>> for Error<T> { | ||
fn from(error_stack: error_stack::Report<T>) -> Self { | ||
let error = *error_stack.current_context(); | ||
Self { | ||
error, | ||
// cursed way to do this without cloning | ||
report: serde_json::from_str(&serde_json::to_string(&error_stack).unwrap()).unwrap(), | ||
error_stack, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.