Giter Club home page Giter Club logo

defekt's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar dotkuro avatar goloroden avatar grundhoeferj avatar nidomiro avatar semantic-release-bot avatar yeldirium avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

defekt's Issues

Remove dependency on humanize-string

The package humanize-string uses an old version of the package decamelize. Probably because of breaking changes, humanize-string uses [email protected], while the newest version is 5.0.0.

This is a problem because the old version of decamelize uses xregexp, which has issues when being transpiled for IE11, i.e. it produces an infinite loop. decamelize stopped using xregexp in around version 4.0.0

We don't usually change things to support IE11 and I hate that that is the reason why I am proposing this, but this change has other good side-effects. humanize-string does much more than we need. We use it to produce readable strings from error names, which is a pretty tiny subset of what humanize-string can do and which we can easily do ourselves.

So we should remove humanize-string and replicate it's behavior ourselves.

Add unwrapErrorOrThrow

Mainly for testing purposes but also surely for other use cases I want to add a unwrapErrorOrThrow function to Results, so that in cases where we know that something fails we can access the error directly without having to write an if statement.

data attribute might not always be optional

What is this issue about?

At the moment you can set a type for data attribute of a custom error, but it will still always be optional. This seems like an unnecessary restriction. By default the type of data is any, so therefore optional anyway and if you want it to be optional for the custom type you provide, you could just write it explicitly like t | undefined. So I think we can safely make it the data attribute required.

Migrate to TypeScript

Once we migrate to TypeScript (this primarily depends on migrating roboter to TypeScript), we can use this implementation:

import humanizeString from 'humanize-string';

export interface ICustomError extends Error {
  name: string;
  code: string;
  message: string;
  cause?: Error;
}

type ErrorConstructors<T> = {
  [key in keyof T]: new(message?: string, cause?: Error) => ICustomError
};

const defekt = function <T extends {
  [ key: string ]: { code?: string };
}> (errorDefinitions: T): ErrorConstructors<T> {
  const errors: Partial<ErrorConstructors<T>> = {};

  /* eslint-disable guard-for-in */
  for (const errorName in errorDefinitions) {
    const errorDefinition = errorDefinitions[errorName];

    if (!errorDefinition) {
      continue;
    }

    const { code = `E${errorName.toUpperCase()}` } = errorDefinition;

    errors[errorName] = class extends Error implements ICustomError {
      public name: string;

      public code: string;

      public message: string;

      public cause?: Error;

      /* eslint-disable no-loop-func */
      public constructor (message = `${humanizeString(errorName)}.`, cause?: Error) {
        super();

        this.name = errorName;
        this.code = code;
        this.message = message;
        this.cause = cause;
      }
      /* eslint-enable no-loop-func */
    };
  }
  /* eslint-enable guard-for-in */

  return errors as ErrorConstructors<T>;
};

export default defekt;

ErrorConstructor is not exported

After we define errors with defekt, we create error instances by using an ErrorConstructor. This is our own type and not to be confused with node's ErrorConstructor. Sometimes, we want to create errors dynamically, and for that we want to pass the ErrorConstructors around. At the moment, our own type is not exported, so when we pass the constructors generated by defekt around, they default to the built-in ErrorConstructor type, widening the type obtained by invoking the constructor from CustomError to Error.

I think we should export our own ErrorConstructor as well.

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.