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

feat: using neverthrow to explicitly handle errors in UserController #371

Merged
merged 41 commits into from
Oct 12, 2020

Conversation

karrui
Copy link
Contributor

@karrui karrui commented Sep 24, 2020

Problem

Similar to #332, this is an extension PR for #142 to implement explicit error handling to the endpoints.

Solution

Improvements:

  • Add explicit handling using neverthrow's FP-like Result class to return either success oks or error tracks for entire User module endpoints

Note that sms related files should technically be updated to be in their own module, and the other sms-related functions should also be converted to use Result. However, that is outside the scope of this PR and should be done in their respective PRs.

  • Add lint ordering rule for test root import so test imports are less messy
  • Inline celebrate rules in user.routes like auth routes
  • Remove MalformedOtpError, it has now been replaced by ApplicationError
  • Add utils/hash containing the neverthrown versions of bcrypt.hash and bcrypt.compare

Tests

  • Add express-auth helper function to get authed session for integration tests that requires the user to be logged in.
  • User tests are moved to modules/user/ __test__ directory
  • Tests are refactored to use new FPfied methods for UserService and SmsService
  • Add integration tests for entire /user route
  • Rewrite SmsService tests using Jest and TypeScript

@karrui karrui changed the base branch from develop to feat/track-user-login September 24, 2020 05:40
@karrui karrui marked this pull request as draft September 29, 2020 03:01
@karrui
Copy link
Contributor Author

karrui commented Sep 29, 2020

Drafting this PR to change how controllers call services; they should be called step by step for additional clarity, checking errors every step of the way as discussed with Yuan

import { Router } from 'express'

import * as UserController from './user.controller'
import * as UserRules from './user.rules'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a new preference for inlining the rules?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tbh, they are just used in this place, and having to refer to yet another *.rules file when in *.routes files seems a little annoying when developing or reviewing

Base automatically changed from feat/track-user-login to develop September 29, 2020 03:18
@karrui karrui marked this pull request as ready for review September 29, 2020 07:33
@karrui karrui marked this pull request as draft September 29, 2020 07:34
@karrui
Copy link
Contributor Author

karrui commented Sep 29, 2020

Sorry, sticky fingers. Not refactored to check for errors every step of the way yet.

@karrui karrui marked this pull request as ready for review September 29, 2020 07:50
@karrui
Copy link
Contributor Author

karrui commented Sep 29, 2020

Add error handling every step of the way.

src/app/utils/hash.ts Show resolved Hide resolved
src/app/modules/auth/auth.service.ts Show resolved Hide resolved
src/app/modules/user/user.service.ts Show resolved Hide resolved
src/app/modules/user/user.utils.ts Outdated Show resolved Hide resolved
tests/integration/helpers/express-auth.ts Show resolved Hide resolved
tests/unit/backend/services/sms.service.spec.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@mantariksh mantariksh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some remaining questions about backwards compatibility

src/app/modules/user/user.controller.ts Outdated Show resolved Hide resolved
src/app/modules/user/user.controller.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@mantariksh mantariksh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

èl géê tëē ėm

@karrui karrui merged commit 293e843 into develop Oct 12, 2020
@karrui karrui deleted the feat/neverthrow-user branch October 12, 2020 06:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants