Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

better error handling #12

Closed
JohnDoePBabu opened this issue Oct 29, 2020 · 7 comments
Closed

better error handling #12

JohnDoePBabu opened this issue Oct 29, 2020 · 7 comments
Assignees
Labels
feature Issue/Pull Request Label for New Features
Milestone

Comments

@JohnDoePBabu
Copy link
Collaborator

I noticed that currently errors are neither captured, handled, or propagated.
This could be replicated by calling any method with an invalid token (there are many other ways, of course).

if (!error & response.statusCode === 200) { callback(JSON.parse(body)) }

Also, we aren't following the NodeJS error-first-callback practice.

We could modify this by introducing a breaking change and passing the error as the first argument of the callback.

callback(err, JSON.parse(body))
or make it backward compatible by passing error as the second argument. This would be a departure from the usual node JS callback conventions though.

thoughts?

@warengonzaga
Copy link
Owner

@JohnDoePBabu what is your best approach for handling error? I didn't add the error handling while making the project since I want to understand how BMC API works. Do you have any suggestion since we are going to use GOT might has better error handling than request.

@warengonzaga
Copy link
Owner

Hi @JohnDoePBabu I'm assigning this to you...

@warengonzaga warengonzaga added the feature Issue/Pull Request Label for New Features label Nov 2, 2020
@warengonzaga warengonzaga changed the title BMCJS does not handle request failure Better Error Handling Nov 2, 2020
@JohnDoePBabu
Copy link
Collaborator Author

@warengonzaga We can do one of the below

  1. Catch error from Axios and pass the error as the first callback
        try {
            const response = await requester.get(path, {
                headers: {
                    Authorization: 'Bearer ' + this.access_token,
                }
            });
            callback(null, response.data); 
        } catch (err) {
            callback(err);
        }

Pros:

  • With callback and Node JS, error first is the real recognized approach.

Cons:

  • For people currently using the library, this will be a very big breaking change.
    if they are currently using the module as
coffee.Subscriptions((data) => {
    console.log(JSON.stringify(data));
  });

They will have to change to

 if (err) // do something with error
 else console.log(JSON.stringify(data));
  });
  1. Catch error from Axios and pass the error as a second callback
 if (err) // do something with error
 else console.log(JSON.stringify(data));
  });

Pros:

  • Backward compatible with the previous version of BMC JS. No breaking change for current users.
    Cons:
  • Against Node JS convention.
  • counter-intuitive for future users
  1. Move to Promise style of handling error by rejecting

        try {
            const subcriptions = await coffee.Subscriptions();
        } catch (err) {
            // do something with error
        }

Pros: cleaner than callback
Cons: Again breaking change

  1. Support both callback and promise
    Check if the user has sent a function as a second argument if so return callback else use a promise.
    Again for the callback version, we will have to decide if we want option 1 or option 2.
    So the user can do both
coffee.Subscriptions((err, data) => {
 if (err) // do something with error
 else console.log(JSON.stringify(data));
  });

and ...

        try {
            const subcriptions = await coffee.Subscriptions();
        } catch (err) {
            // do something with error
        }

@warengonzaga
Copy link
Owner

What do you think is better @JohnDoePBabu? and more standard?
What I see your option number 3 is more okay to me. Breaking changes is natural since we are planning to release the v2.
We need clear documentation once these changes implemented so they can easily adapt to the latest version.

@JohnDoePBabu
Copy link
Collaborator Author

Hi @warengonzaga,

I noticed that when there is no data, eg: no supporters, BMC API sends a 200 response with an error in the response body.
such as
{ "error": "No subscriptions" }
Shall we implement it the same way or do you prefer to

  • send an empty array
  • throw an error

@warengonzaga
Copy link
Owner

Hi @JohnDoePBabu,

I would prefer to throw an error message instead of empty array.

@JohnDoePBabu JohnDoePBabu mentioned this issue Jan 2, 2021
@JohnDoePBabu JohnDoePBabu mentioned this issue Jan 9, 2021
warengonzaga added a commit that referenced this issue Jan 9, 2021
Better Error Handling closed #12
@warengonzaga
Copy link
Owner

All good closing this issue with merge #18

@warengonzaga warengonzaga changed the title Better Error Handling better error handling Dec 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Issue/Pull Request Label for New Features
Projects
None yet
Development

No branches or pull requests

2 participants