JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 299
  • Score
    100M100P100Q90077F
  • License MPL-2.0

A simple wrapper to have go-like error handling

Package Exports

  • @1ry/golike-errors

Readme

Go-like errors

a foolish approach to error handling utilizing typescript.

examples

const result: Result<number, string> = {
   success: true,
   data: 42,
};

if (result.success) {
    // result is { success: true; data: number }
    console.log(result.data);
} else {
    // result is { success: false; error: string }
    console.error(result.error);
}

or

type ParseError = {
    code: "INVALID_JSON";
    message: string;
};

function parseJson(text: string): Result<unknown, ParseError> {
    try {
        return {
            success: true,
            data: JSON.parse(text),
        };
    } catch {
        return {
            success: false,
            error: {
                code: "INVALID_JSON",
                message: "Failed to parse JSON.",
            },
        };
    }
 }