Code Monkey home page Code Monkey logo

Comments (3)

tafelito avatar tafelito commented on May 17, 2024

Btw, what's the best way to share actions through different methods. Let's say you have a meteor method that has 3 steps (actions) and then there is another method that needs to call 1 of the same actions, do you take it out to a different file and then export it? Do you put it within the imports/api or you put it on import/modules? Do you pass the promise through in case you need to reject it if an error happens?

Thanks

from pup.

cleverbeagle avatar cleverbeagle commented on May 17, 2024

Hi @tafelito customizing the error code in your own is best. The 500 is generic enough to alert that something went wrong on the server. Is the change you're proposing to pass the full exception to the client?

Re: sharing actions, this depends. You can do an export const of a specific method inside of an action, or, if it's something a bit more complex, just take that code and put it in its own file, importing it into both the action and the method. Both work but it's really a judgment call based on the work you're doing/how often the code will be reused.

from pup.

tafelito avatar tafelito commented on May 17, 2024

thanks @cleverbeagle for the answer. What I meant by customizing the error code is that when you throw an Meteor.Error from an action, then you are catching it on the method and throwing a new Meteor.Error with error code 500, so the cliente always get 500, not the one thrown in a specific action. Not sure If I'm being clear here

So if I have this

const action1 = ({ ... }) => {
  try {
    throw new Meteor.Error('600', 'Error msg');
  } catch (exception) {
    action.reject(`[handler.action1] ${exception}`);
  }
};

const handler = ({ ... }, promise) => {
  try {
    action = promise;

    action1(...);
    action2(...); //this will be executed 
    action.resolve();
  } catch (exception) {
    action.reject(`[handler] ${exception}`);
  }
};

Meteor.methods({
  method(...) {
    return handler({ ... })
      .then(response => response)
      .catch((exception) => {
       throw new Meteor.Error('500', exception); // always returns error code 500, not 600
      });
  },
});

So right now, instead of sending the message from the reject, I return an object, with the message, and the exception itself and then in the catch I do this

  .catch(({ exception, message }) => {
    throw new Meteor.Error(exception.error, message);
  });

It should have an extra validation though, in case the exception is not an actual Meteor.Error

from pup.

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.