Closed Bug 1437191 Opened 8 years ago Closed 7 years ago

res.reportError should raise an exception

Categories

(Taskcluster :: Services, enhancement)

enhancement
Not set
normal

Tracking

(Not tracked)

RESOLVED FIXED

People

(Reporter: dustin, Assigned: arshadkazmi42, Mentored)

References

Details

(Keywords: good-first-bug)

We currently rely on callers to return after calling `res.reportError`. We might as well enforce that by throwing an exception which tc-lib-api can then catch in the handler wrapper.
Assignee: nobody → bstack
Brian: are you still working on this?
Flags: needinfo?(bstack)
Not at the moment
Assignee: bstack → nobody
Flags: needinfo?(bstack)
Mentor: dustin
Keywords: good-first-bug
:dustin I would like to take this up. Is this a mentored bug? I would like some help getting started.
Flags: needinfo?(dustin)
Sure! The reportError function is defined here: https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/errors.js and the error-handling is here: https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/handle.js#L24 So the thing that is called at handle.js line 13 might call res.reportError, and might forget to return after the call. The fix is to throw an error in res.reportError, and then catch that error in handle.js and return gracefully.
Flags: needinfo?(dustin)
I am new to Java Script and this seems a bit unfamiliar. Can you provide some pointers as to how can i get started?
One good way to approach something like this is to do test-driven-development: find the tests for the relevant bits and change them to represent the expected behavior. Then figure out why things break. For example, this test https://github.com/taskcluster/taskcluster-lib-api/blob/920a1094511dc73491e952202bd912d3d4decfb1/test/errors_test.js#L24 tests reportError. Try adding a line like `console.log('uhoh');` after the `res.reportError`, then run the tests and see that the console output is printed. The bug is to make that not happen :)
Is this issue still open? I would like to work on this
Flags: needinfo?(dustin)
Sure :) BTW, you don't need to set the needinfo flag every time you comment in Bugzilla.
Assignee: nobody → arshadkazmi42
Flags: needinfo?(dustin)
As per Comment 4 I tried adding throw `throw {code, message, requestInfo};` here https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/errors.js#L75 on this, it is going to this catch block. https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/handle.js#L29 Can you help me understand, how exactly we are planning to handle it in handle.js? Or even my throw state is at the correct place or not.
Flags: needinfo?(dustin)
In handle.js, you'll want to do something similar to what is done in errors.js now -- res.status(..).json(). However, I see that handle.js is calling res.reportError as well. So you may need to do a small amount of refactoring to make that work -- I'll leave it to you to find a nice solution to that.
Flags: needinfo?(dustin)
I did few changes. Changed this line of code https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/errors.js#L75 to `throw ({status, code, message, requestInfo});` and changed this https://github.com/taskcluster/taskcluster-lib-api/blob/master/src/middleware/handle.js#L34 to `return res.status(err.status).json(err);` The case mentioned in Comment 6 is done. The log after res.reportError is not getting logged in console. But many of the other test cases are failing. Is this fix correct, or there is something missing in this?
Flags: needinfo?(dustin)
It sounds like it's not correct :) For the next round, please file a pull request with your changes so that I can take a look.
Flags: needinfo?(dustin)
I have created a PR here https://github.com/taskcluster/taskcluster-lib-api/pull/147 But many tests are failing. If you can check it and let me know, if the fix is correct or not Also how can I handle other tests, I need to change the tests or make some more changes in the fix
Flags: needinfo?(dustin)
Flags: needinfo?(dustin)
Releasing 12.8.0, and I'll deploy that in hooks.
Seems to be working. I'm happy for the other services to get upgraded as we move to a monorepo :)
Status: NEW → RESOLVED
Closed: 7 years ago
Resolution: --- → FIXED
Depends on: 1518137
Component: Platform Libraries → Services
You need to log in before you can comment on or make changes to this bug.