Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Auto errors

Auto class errors

Note

Check out the example: TS

Graou can edit your class to automaticly implements the whole concepts without the need to do much things.

Important

Only the owned methods of the class are decorated. Inherit methods are not altered.

import graou from "@carthage-js/graou";

const errorsFactory = graou.makeModuleErrorsFactory({
  moduleName: "<YOUR PROJECT NAME>",
});

class MyClass {
  myMethod() {
    // ... my code
  }

  async myAsyncMethod() {
    // ... my code
  }
}

const MyClassDecorated = graou.utils.decorateClassWithErrors(errorsFactory, MyClass);

// Usage
const instance = new MyClassDecorated();
instance.myMethod();

In this example, the resulting class is attach to Errors where the scope is MyClass with 3 codes: constructor, myMethod, myAsyncMethod. Each code matching a method of the class. You got a constructor code no matter what because they are implicitly declarated by the js class. Both methods and constructor on the decorated use the trap mecanism to handle errors. The decorated class define a symbol for each method to handle recursive method.

Graou offer methods to easily access errors that got attached to the class and methods:

const errors = graou.utils.getClassErrors(MyClassDecorated);
const error = graou.utils.getMethodError(MyClassDecorated.prototype.myMethod);

If you allow yourself to use experimentalDecorators on your typescript project then it’s even easier:

import graou from "@carthage-js/graou";

const errorsFactory = graou.makeModuleErrorsFactory({
  moduleName: "<YOUR PROJECT NAME>",
});

@graou.decorators.AutoErrors(errorsFactory)
class MyClass {
  myMethod() {
    // ... my code
  }

  async myAsyncMethod() {
    // ... my code
  }
}

// Usage
const instance = new MyClass();
instance.myMethod();

Note


You can pass directly an errors that match your class because it can be much easier for the typing over the usage. You must aware that errors object must match your class declaration. Otherwise, Graou throw an error due to the mismatch between the two objects.

Bind class errors

Note

Check out the example: TS

Graou offer also an alternate way to alterate your class. The whole reason is due to typing. Make the errors fully generated mean that you must rely upon getClassErrors and getMethodError function to access the basic concepts of Graou for those class. There are not bad but they can create some dumb error because the things are solved during runtime. To avoid that, you can define separetly the class and errors. This way typescript will be able to ensure the typing. Other tools will also be able to issue early error if something is undefined (like webpack). Graou is gonna check the class and errors are matching when he decorate the class with them to avoid issues.

import graou from "@carthage-js/graou";

const errorsFactory = graou.makeModuleErrorsFactory({
  moduleName: "<YOUR PROJECT NAME>",
});

const errors = errorsFactory("MyClass", ["myMethod", "myAsyncMethod"]);

class MyClass {
  myMethod() {
    // ... my code
  }

  async myAsyncMethod() {
    // ... my code
  }
}

const MyClassDecorated = graou.utils.bindClassWithErrors(errors, MyClass);

// Usage
const instance = new MyClassDecorated();
instance.myMethod();