Git Product home page Git Product logo

error's Introduction

Rustic Error

Robust and type-safe error management inspired by Rust's Result pattern.

Example

import type { Result } from "rustic-error";
import { ok, error } from "rustic-error"; 

function divide(a: number, b: number): Result<number, string> {
  if (b === 0) return error("Division by zero");

  return ok(a / b);
}

const foo = divide(1 / 2);

if (foo.ok) {
  console.log("Result", foo.value);
} else {
  console.log("Error", foo.error);
}

error's People

Contributors

bluzzi avatar

Stargazers

Léo Raclet avatar  avatar hcampos avatar  avatar Unarray avatar Gaëtan H avatar Chris avatar

Watchers

 avatar

error's Issues

Type check before callable ignore in callable

image

test.ts :

import {resultify} from "rustic-error";

type a = {
    bar(): string;
}
type b = {
    foo: a|null
}

const bFoo: b = {
    foo: {
        bar(): string {
            return 'Joe doe'
        }
    }
}
if (bFoo.foo !== null) {
    const bar1 = bFoo.foo.bar();
    const bar2 = resultify(() => bFoo.foo.bar());
}

tsconfig.json :

{
  "compilerOptions": {
    "target": "es2022",
    "module": "commonjs",
    "strict": true,
  }
}

solution that can remove error, but not pratical :

if (bFoo.foo !== null) {
    const bar1 = bFoo.foo.bar();
    const foo = bFoo.foo
    const bar2 = resultify(() => foo.bar());
}

Ability for the resultify function to handle synchronous code

Added the ability for the resultify function to handle synchronous code without returning a promise.

This will keep our function synchronous without having to make it asynchronous for no reason.

const myFunction = (value: string): string => {
  if (value.length < 4) throw new Error("Value is too short!")

  // Perform operations on the value 🤷‍♂️

  return value
}

const mySyncFunction = (value: string): string => {
  const result = resultify(() => myFunction(value));

  if(!result.ok) return "hello!";

  return result.value
}

// For exemple, i can use my function at top-level
console.log(mySyncFunction ("hey"));
With current version
const myFunction = (value: string): string => {
  if (value.length < 4) throw new Error("Value is too short!")

  // Perform operations on the value 🤷‍♂️

  return value
}

const myAsyncFunctionButItIsSync = async(value: string): Promise<string> => {
  const result = await resultify(() => myFunction(value));

  if(!result.ok) return "hello!";

  return result.value
}

// For example, I can't use my theoretically synchronous function at the top-level
(async() => {
  console.log(await myAsyncFunctionButItIsSync("hey"));
})();

Use case:

type MyFooObject = {
  barMethod: (callback: (value: string) => string) => void;
};

const fooObject: MyFooObject;

fooObject.barMethod((value) => {
  const myOperation = resultify(() => something(value));

  if (!myOperation.ok) {
    return 'a return example';
  }

  return myOperation.value;
});

// return type of `MyFooObject.barMethod` doesn't match `Promise<string>`

Possible solution:

Create a second function exactly the same, which this time would not take a MaybePromise<T> but T, and return T directly.

Current:

export async function resultify<T>(fn: (...params: unknown[]) => MaybePromise<T>): Promise<Result<T, Error>> {
  ...
}

Possible solution:

export async function syncResultify<T>(fn: (...params: unknown[]) => T): Result<T, Error> {
  ...
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.