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

fix: instanceof MedusaError does not work #9094

Merged
merged 4 commits into from
Sep 11, 2024

Conversation

McTom234
Copy link
Contributor

Closes #9088

As stated in the issue and this block article, adding one line of code, fixes the instanceof method.

What

Set the name of the MedusaError type.

Why

Developers can differ between MedusaErrors and other errors easily.

I already have an idea/suggestion to improve the default error handler with this new possibility (see below).

Testing

Somewhere in your code, throw a MedusaError and a generic Error. Try this with and without this fix. For both, get the instanceof Error and instanceof MedusaError.

Before the fix, both return true for Error. After, the medusa error will additionally return true for the MedusaError.

Because of that, no breaking changes were made.

Further possibilities

The current behavior of the errorHandler for generic/non-medusa-errors is to return a 500 with message An unknown error occurred.
This is cool in most cases. Some developers might prefer to get the actual error message returned, if it is not a MedusaError. The instanceof can be used in that case. This configuration could be added via the medusa config or via the parameters for the default errorHandler function.

This shall not be part of this PR and just an insight on why this might be useful.

Copy link

changeset-bot bot commented Sep 11, 2024

⚠️ No Changeset found

Latest commit: dd16c22

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

Copy link

vercel bot commented Sep 11, 2024

@McTom234 is attempting to deploy a commit to the medusajs Team on Vercel.

A member of the Team first needs to authorize it.

@McTom234 McTom234 changed the title fix instanceof MedusaError does not work fix: instanceof MedusaError does not work Sep 11, 2024
Copy link
Contributor

@riqwan riqwan left a comment

Choose a reason for hiding this comment

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

LGTM, some specs are failing!

@McTom234
Copy link
Contributor Author

@riqwan Oh, I see, at least one test fails, maybe some more. The default name of the error will change. As you can test locally, the instanceof will remain functional.

As you test for the name of the error, do you rely on that somewhere in the code or should I just update the integration tests?

@riqwan
Copy link
Contributor

riqwan commented Sep 11, 2024

As you test for the name of the error, do you rely on that somewhere in the code or should I just update the integration tests?

I think this is a safe change, you just need to update the test.

We handled this issue differently ourselves - ref. I don't know if that will be translatable here. Will loop in @adrien2p for a second opinion.

@@ -46,6 +46,8 @@ export class MedusaError extends Error {
constructor(type: string, message: string, code?: string, ...params: any) {
super(...params)

this.name = this.constructor.name
Copy link
Member

Choose a reason for hiding this comment

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

I would suggest to do the following to follow our practices and be consistent with the rest of the code base

export class MedusaError extends Error {
  // we do not use Symbol because it is not serializable
  __isMedusaError = true

  public type: string
  public message: string
  public code?: string
  public date: Date
  public static Types = MedusaErrorTypes
  public static Codes = MedusaErrorCodes

  static isMedusaError(error: any): error is MedusaError {
    return !!error.__isMedusaError
  }

and then when checking somewhere you can do MedusaError.isMedusaError(err)

wdyt?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You have your point 👍

In that case, I would add a hint in the docs as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure, open that as a different PR as it requires different reviewers.

@McTom234
Copy link
Contributor Author

@riqwan I applied the changes. Could you run the pipelines again?

@McTom234
Copy link
Contributor Author

Seems good to go @riqwan 😄

@riqwan
Copy link
Contributor

riqwan commented Sep 11, 2024

@McTom234 legend! Thanks for the contribution!

@riqwan riqwan merged commit 5651e89 into medusajs:develop Sep 11, 2024
14 of 22 checks passed
@McTom234 McTom234 deleted the fix/medusa-error-constructor-name branch September 11, 2024 15:26
kodiakhq bot pushed a commit that referenced this pull request Sep 12, 2024
The same as #9094, but for v1. Our project won't be moving to v2 soon probably, and we want the feature of course 😄 

Co-authored-by: Riqwan Thamir <5105988+riqwan@users.noreply.github.com>
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.

[bug] instanceof MedusaError does not work
3 participants