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

Clean up old errors #3633

Merged
merged 29 commits into from
May 11, 2023
Merged

Clean up old errors #3633

merged 29 commits into from
May 11, 2023

Conversation

thomasheartman
Copy link
Contributor

@thomasheartman thomasheartman commented Apr 26, 2023

This PR attempts to improve the error handling introduced in #3607.

About the changes

tl;dr:

  • Make UnleashError constructor protected
  • Make all custom errors inherit from UnleashError.
  • Add tests to ensure that all special error cases include their relevant data
  • Remove PasswordMismatchError and BadRequestError. These don't exist.
  • Add a few new error types: ContentTypeError, NotImplementedError, UnauthorizedError
  • Remove the ...rest parameter from error constructor
  • Add an unexported GenericUnleashError class
  • Move OpenAPI conversion function to BadDataError clas
  • Remove explicit Error.captureStackTrace. This is done automatically.
  • Extract getPropFromString function and add tests

In a more verbose fashion

The main thing is that all our internal errors now inherit fromUnleashError. This allows us to simplify the UnleashError constructor and error handling in general while still giving us the extra benefits we added to that class. However, it does also mean that I've had to update all existing error classes.

The constructor for UnleashError is now protected and all places that called that constructor directly have been updated. Because the base error isn't available anymore, I've added three new errors to cover use cases that we didn't already have covered: NotImplementedError, UnauthorizedError, ContentTypeError. This is to stay consistent in how we report errors to the user.

There is also an internal class, GenericUnleashError that inherits from the base error. This class is only used in conversions for cases where we don't know what the error is. It is not exported.

In making all the errors inherit, I've also removed the ...rest parameter from the UnleashError constructor. We don't need this anymore.

Following on from the fixes with missing properties in #3638, I have added tests for all errors that contain extra data.

Some of the error names that were originally used when creating the list don't exist in the backend. BadRequestError and PasswordMismatchError have been removed.

The BadDataError class now contains the conversion code for OpenAPI validation errors. In doing so, I extracted and tested the getPropFromString function.

Main files

Due to the nature of the changes, there's a lot of files to look at. So to make it easier to know where to turn your attention:

The changes in api-error.ts contain the main changes: protected constructor, removal of OpenAPI conversion (moved into BadDataError.

api-error.test.ts contains tests to make sure that errors work as expected.

Aside from get-prop-from-string.ts and the tests, everything else is just the required updates to go through with the changes.

Discussion points

I've gone for inheritance of the Error type over composition. This is in large part because throwing actual Error instances instead of just objects is preferable (because they collect stack traces, for instance). However, it's quite possible that we could solve the same thing in a more elegant fashion using composition.

For later / suggestions for further improvements

The api-error files still contain a lot of code. I think it might be beneficial to break each Error into a separate folder that includes the error, its tests, and its schema (if required). It would help decouple it a bit.

We don't currently expose the schema anywhere, so it's not available in the openapi spec. We should look at exposing it too.

Finally, it would be good to go through each individual error message and update each one to be as helpful as possible.

@vercel
Copy link

vercel bot commented Apr 26, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
unleash-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 11, 2023 8:10am
unleash-monorepo-frontend ✅ Ready (Inspect) Visit Preview 💬 Add feedback May 11, 2023 8:10am

@thomasheartman thomasheartman marked this pull request as draft April 26, 2023 14:28
@github-actions
Copy link

github-actions bot commented May 8, 2023

After enabling strictNullChecks this PR would be increasing the number of null check errors from 294 to 296.
Make sure your branch is up-to-date with main and check the diff in the console output to pinpoint the offending files.

@github-actions
Copy link

github-actions bot commented May 8, 2023

After enabling strictNullChecks this PR would be increasing the number of null check errors from 294 to 296.
Make sure your branch is up-to-date with main and check the diff in the console output to pinpoint the offending files.

@github-actions
Copy link

github-actions bot commented May 8, 2023

After enabling strictNullChecks this PR would be increasing the number of null check errors from 294 to 296.
Make sure your branch is up-to-date with main and check the diff in the console output to pinpoint the offending files.

Copy link
Contributor

@kwasniew kwasniew left a comment

Choose a reason for hiding this comment

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

I started reviewing the first two points from the TL;DR and found something that should be addressed before I continue the review so I'm submitting a request changes proposal.

src/lib/error/api-error.ts Outdated Show resolved Hide resolved
src/lib/error/disabled-error.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@kwasniew kwasniew left a comment

Choose a reason for hiding this comment

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

Very nice improvements. Non blocking approve with some comments.

Copy link
Contributor

@kwasniew kwasniew left a comment

Choose a reason for hiding this comment

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

Very nice improvements. Non blocking approve with some comments.

@thomasheartman thomasheartman merged commit 9943179 into main May 11, 2023
13 checks passed
@thomasheartman thomasheartman deleted the chore/clean-errors branch May 11, 2023 09:10
thomasheartman added a commit that referenced this pull request May 15, 2023
This PR adds the missing serialization of the AuthenticationRequired
response back in. It was mistakenly removed in #3633.

This PR also adds another test to verify that it the options property is
present.
nunogois added a commit that referenced this pull request May 24, 2023
https://linear.app/unleash/issue/2-1085/bug-password-based-login-still-shows-on-the-login-page-even-if

Fixes a regression introduced with the changes related with #3633 where
we still show the password login even though it's disabled.

---------

Co-authored-by: Thomas Heartman <thomas@getunleash.io>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants