Giter VIP home page Giter VIP logo

Comments (5)

daffl avatar daffl commented on June 3, 2024 4

One way this is already possible would be to .catch the promise and return a normal value instead of throwing another error. So in the hook you could add a result property to the error object, then check for it in the catch and return it like so:

app.service('myservice').create().catch(error => {
  if(error.result) {
    return error.result;
  }

  throw error;
});

I think it wouldn't be very hard to add as a feature either. Basically we just have to change https://github.com/feathersjs/feathers-hooks/blob/master/src/hooks.js#L105 to

.then(hook => {
  if(hook.error) {
    return Promise.reject(hook.error);
  }

  return hook.result;
});

from feathers-hooks.

daffl avatar daffl commented on June 3, 2024 1

This will be available in the next version of Feathers core which will include feathers-hooks.

Closed via feathersjs/feathers#621

from feathers-hooks.

beeplin avatar beeplin commented on June 3, 2024

Or like in before hooks: if in error hook the hook.result is set, then the error is swallowed, and the hook.result is returned as if the service method succeeds as normal.

from feathers-hooks.

beeplin avatar beeplin commented on June 3, 2024

@daffl Yes in old days without error hooks, I was using .catch to do this. However, .catch has to be written after the method calling code (as your example shows), and it is way better to hide this kind of logic into error hooks so the users of the service just call app.service('xxx') and no need to care about the .catch thing.

from feathers-hooks.

beeplin avatar beeplin commented on June 3, 2024

nice~!

from feathers-hooks.

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.