refactor: return custom error from utils when bcrypt fails #1426
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
src/app/utils/hash
, we have utility functions which wrap calls tobcrypt.hash
andbcrypt.compare
inResultAsync
. However, we return a genericApplicationError
when these functions fail. This means that downstream functionality has to deal with the error as a genericApplicationError
without context of where it came from, e.g. when deciding what status code and message to return inmapRouteError
. Developers have to search through all the service functions called in controllers one by one until they find where theApplicationError
was returned.generateOtp
utility, but all the times when we need to call it, we have to create not just the OTP but a hash of it as well. Since these two bits of functionality always come together, it makes sense to have a utility function which does both.Part of #941
Solution
HashingError
whenbcrypt
fails. This makes the origin of the error much clearer.generateOtpWithHash
utility function, so we can callgenerateOtpWithHash().andThen...
instead ofconst otp = generateOtp(); hashData(otp).andThen...
. Note thatverification.service
was left out (it still callsgenerateOtp
instead ofgenerateOtpWithHash
) as it will be addressed together with the rest of Convert verification service to use neverthrow #941.