[No QA] Remove throw in registerNetworkResponseHandler. Clean up IOU actions file.#8087
[No QA] Remove throw in registerNetworkResponseHandler. Clean up IOU actions file.#8087
Conversation
|
cc @mountiny if you are interested in the review here and the changes as they relate to the recent work you did |
|
Is this still WIP? |
src/libs/actions/IOU.js
Outdated
| const iouReportsToUpdate = {}; | ||
|
|
||
| _.each(reports, (reportData) => { | ||
| // First, the existing chat report needs updated with the details about the new IOU |
There was a problem hiding this comment.
| // First, the existing chat report needs updated with the details about the new IOU | |
| // First, the existing chat report needs to be updated with the details about the new IOU |
src/libs/actions/IOU.js
Outdated
| hasOutstandingIOU: true, | ||
| }; | ||
|
|
||
| // Second, the IOU report needs updated with the new IOU details too |
There was a problem hiding this comment.
| // Second, the IOU report needs updated with the new IOU details too | |
| // Second, the IOU report needs to be updated with the new IOU details too |
src/libs/actions/IOU.js
Outdated
| .then(() => Onyx.merge(ONYXKEYS.IOU, {loading: false, creatingIOUTransaction: false})) | ||
| .catch(() => Onyx.merge(ONYXKEYS.IOU, {error: true})); | ||
| .then(() => Onyx.merge(ONYXKEYS.IOU, {loading: false, creatingIOUTransaction: false})); |
There was a problem hiding this comment.
How is error here being handled?
There was a problem hiding this comment.
Which error do we need to handle? Where's it at?
There was a problem hiding this comment.
I guess these maybe? https://github.com/Expensify/Auth/blob/d3cf26e0330c3a4cf98c6ac3abf141a53f617f33/auth/command/CreateIOUSplit.cpp#L15-L18
Should hit the standard handler https://github.com/Expensify/Web-Expensify/blob/7ecf0d26981bcf715f551c58a21744750c06fc83/lib/Auth.php#L115
And because they are 404 and we removed the default handler that rejects the promise we must now do something?
There was a problem hiding this comment.
Yes, seems like maybe anywhere that we have removed a .catch() should have to look at the response jsonCode just in case. Will try to find all these places 😄
Julesssss
left a comment
There was a problem hiding this comment.
Nice refactor and clean up. We'll need some tests, but the changes look good to me so far
|
Actually, going to scale back these changes so they are easier to test. I think we are going to have to fix this problem bit by bit and be sure errors are handled before removing Gonna start with the IOU stuff only for now. |
| // It's a failure, so reject the queued request | ||
| queuedRequest.reject(response); | ||
| return; | ||
| } |
There was a problem hiding this comment.
There may be consequences of removing this code for some API that are wrapped in a catch(), but it will be too much work to look at all API methods using a catch and find out whether it throws these code. I'm only going the fix the ones that are explicitly being handled.
| return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, iouReportsToUpdate); | ||
| Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, iouReportsToUpdate); | ||
| }) | ||
| .catch(() => Onyx.merge(ONYXKEYS.IOU, {error: true})) |
There was a problem hiding this comment.
Are we removing .catch()-es because the jsonCodes from the API are really the only thing we have to go on? Nothing's being thrown in the JS layer that could be caught?
There was a problem hiding this comment.
Can of worms, but generally speaking there's only a couple reasons why you end up in a catch and they are:
fetch() errors
force promise to be rejected by throwing an exception because you are doing something bad in a .then() or intentionally threw a new Error()
For the fetch errors we should be handling them globally in the network layer here for persisted things like report comments
Lines 71 to 76 in 2969ed7
Any persisted fetch() requests should not ever be getting rejected they are just retried or dumped at some point.
For the ones that aren't persisted (one's being modified in this PR) we could trigger the onError() callback here where the request will get retried for a while and then eventually we do throw an error
Lines 236 to 259 in 2969ed7
Which lands us here and rejects the promise (which would lead to an unhandled promise rejection unless we add a catch when we call the method)
Lines 193 to 207 in 2969ed7
So a promise could be rejected in theory. But if we are offline we should not be making the request in the first place.
Having fun yet?
Technically, we could probably argue that in the current state of the app all API methods should be using a catch() because there's always a chance that you were online -> make a request -> go offline -> get a fetch error. But barring any accidental exceptions caused by bad code the only error we should expect to see is the CONST.ERROR.API_OFFLINE error.
If you look at the catches I want to remove here. We can no longer make that assumption because we added other reasons why these promises could be rejected (which I'm undoing) or we explicitly throw errors in the .then() which isn't really necessary and makes the reason behind the .catch() ambiguous. Was it a rare "offline" error or the one we explicitly throw? Do we bother to check?
Basically, this whole business is way more complicated than it has to be and the complexity has led to a lot of non standard error handling.
deetergp
left a comment
There was a problem hiding this comment.
Thanks for the explanation, it definitely cleared things up. I had one NAB comment, but otherwise looks good to me.
| .then((response) => { | ||
| if (response.jsonCode !== 200) { | ||
| throw new Error(`${response.code} ${response.message}`); | ||
| Log.hmmm('Error rejecting transaction', {error: response.error}); |
There was a problem hiding this comment.
NAB: Should we be including the error codes in these log messages?
There was a problem hiding this comment.
Maybe.. I didn't think super hard about this one, but I feel like we should be able to see the request and what error was thrown along with this log line and piece together what happened. This log line felt overly cautious or possibly unnecessary but I wasn't sure.
I think in the future we could consider having a default log any time an action returns a non-200 jsonCode
There was a problem hiding this comment.
I think in the future we could consider having a default log any time an action returns a non-200 jsonCode
It would certainly make debugging easier if we did.
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |


cc @Julesssss
Details
I noticed that for some reason we are throwing an error from deep inside the API lib. These errors should not be thrown and caught with a
.catch().Seeing as we are going to re-use some of the APIs around IOU Reports for workspace chats I'd figure that doing a some cleanup on the actions files couldn't hurt.
Fixed Issues
$ https://github.com/Expensify/Expensify/issues/201109
Tests
CreateIOUTransaction) + Split (CreateIOUSplit) Verify that IOU transactions can be created without issuejsonCodeto be returned by these API when calling the methods (edit Web-Expensify api.php code and try to send405,404, and402verify the correct errors show)Used this diff
Verified the alerts were created
QA