Skip to content

almdeno/safety

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

safety

background information

usage

Exception

import {Exception, ExceptionProps} from 'safety/mod.ts';

ExceptionProps

interface ExceptionProps {
    id: string;
    internalMessageShort: string;
    internalMessageLong: string;
    userMessageShort: string;
    userMessageLong: string;
}

Exception

class Exception {
    constructor(exceptionProps: ExceptionProps) {
        this._id = exceptionProps.id;
        this._internalMessageShort = exceptionProps.internalMessageShort;
        this._internalMessageLong = exceptionProps.internalMessageLong;
        this._userMessageShort = exceptionProps.userMessageShort;
        this._userMessageLong = exceptionProps.userMessageLong;
    }
    
    get errorMessage(); // `Error ID: ${id}. Message: ${internalMessageShort}.`
    get id();
    get internalMessageShort();
    get internalMessageLong();
    get userMessageShort();
    get userMessageLong();
}

Result

import {
    ifOk,
    ifErr,
    isOk,
    isErr,
    ResultOk,
    ResultErr,
    Result,
    Ok,
    Err
} from 'safety/mod.ts';

ResultOk

interface ResultOk<T> {
    isError: false,
    value: T
}

ResultErr

interface ResultErr {
    isError: true,
    error: Error,
    exception: Exception
}

Result

type Result<T> = ResultErr | ResultOk<T>;

Ok

function Ok<T>(value: T): ResultOk<T> {}

Err

function Err(exception: Exception): ResultErr {}

isOk

function isOk<T>(val: Result<T>): boolean {}

isErr

function isErr<T>(val: Result<T>): boolean {}

ifOk

function ifOk<T>(val: Result<T>, cb: (v: T) => void): void {}

ifErr

function ifErr<T>(val: Result<T>, cb: (v: Exception) => void): void {}

Optional

import {Optional, InvalidOptionalGetException} from 'safety/mod.ts';

Optional

class Optional<T> {
    constructor(private readonly value?: T | null) {};
    public get(): Result<T> {}
    public isPresent(): boolean {}
    public isEmpty(): boolean {}
    public ifPresent(func: (val: T) => void) {}
    public orElse(val: T): T {}
    public orElseGet(val: () => T): T {}
}

InvalidOptionalGetException

const InvalidOptionalGetException = new Exception({
    id: 'INVALID_OPTIONAL_GET',
    internalMessageShort: 'Invalid Optional Get.',
    internalMessageLong: 'An invalid optional was accessed using get.',
    userMessageShort: 'Invalid Optional Get.',
    userMessageLong: 'An invalid optional was accessed using get.',
});

About

Exception class for managing application errors.

Resources

Stars

Watchers

Forks

Packages

No packages published