Giter VIP home page Giter VIP logo

Comments (2)

k1r0s avatar k1r0s commented on May 30, 2024

Hi @alexjoverm, thanks for the idea.

This library focus on provide flat options to build more complex stuff.

Now, if you want to handle exceptions in decorated method you can place onException advice to be invoked before main method and you should prevent main method execution using this.stop. You want prevent main method to be invoked because you don't want execute twice.

 // advice will wrap the method to be executed on try, catch block
@beforeMethod(Whatever.OnException) 
myMethod(){  // <- it will not be executed it self
...
}

So, as you suggested, the method is wrapped with try, catch.

  this.stop()
  try {
    meta.result = meta.rawMethod.apply(meta.scope, meta.args)
  } catch (err) {
    // stuff
  }

I'll try to give a standard solution. Anyway this is an example to handle exceptions on decorated method:

https://github.com/k1r0s/kaop-ts/blob/master/test/demos/onExceptionAdvice.spec.ts

from kaop-ts.

k1r0s avatar k1r0s commented on May 30, 2024

This is what implementation would look like

This feature should pass this test:

import { AdvicePool, IMetadata, onException, adviceMetadata } from "../../src/kaop-ts"

class MyAdvicePool extends AdvicePool {
  static handleError (@adviceMetadata meta: IMetadata) {
    console.log(`There was an error in ${meta.propertyKey}(): -> ${meta.err.message}`)
  }
}

class ExceptionTest {

  @onException(MyAdvicePool.handleError)
  // static wrongMethod (callback: number | Function) {
  static wrongMethod (callback: any) {
    callback()
  }
}

describe("kaop-ts demo -> exception join point", () => {
  it("advice should be executed when decorated method throws an exception", (done) => {
    ExceptionTest.wrongMethod(2)
    ExceptionTest.wrongMethod(done)
  })
})

Check the difference between this one and previous test

from kaop-ts.

Related Issues (20)

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.