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

Directly throw Supabase error in server utility functions #412

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

philliphartin
Copy link

@philliphartin philliphartin commented Sep 3, 2024

Remove opinionated use of createError in favor of direct throw for better error handling

Types of changes

  • Bug fix (a non-breaking change which fixes an issue)
  • New feature (a non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Description

This change removes the use of createError and replaces it with a direct throw. This modification allows downstream users more flexibility in catching and handling errors, fitting the specific needs of their applications.

Why is this change required? What problem does it solve?

The previous implementation using createError restricted how errors could be caught and handled by downstream users. By directly throwing errors, developers now have the ability to catch and manage these errors in a way that best suits their application's requirements.

Resolves: N/A

Checklist:

  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added tests to cover my changes (if not applicable, please state why)

Copy link

netlify bot commented Sep 3, 2024

👷 Deploy request for n3-supabase pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 2730ea8

@th1m0
Copy link
Contributor

th1m0 commented Sep 3, 2024

@philliphartin could you provide an example where this would be useful?

@philliphartin
Copy link
Author

philliphartin commented Sep 3, 2024

@th1m0

@philliphartin could you provide an example where this would be useful?

Sure, let's say you're building an API endpoint that handles invitations to join a platform. The endpoint could be capable of determining if the invitation link is being opened by an authenticated user or not with the serverSupabaseUser utility.

Example

const user =  await serverSupabaseUser(event)

If the user is available, then they can associate the invitation to that user.
If the user is null or undefined, then the endpoint author can handle that case.
If there was an actual error with the supabase call, then the try/catch case is invoked and the endpoint author can also handle that accordingly.

@th1m0
Copy link
Contributor

th1m0 commented Sep 4, 2024

Sorry for the late response on this. Currently it is also possible to catch the error thrown with createError, which is provided by Nuxt. That's why I asked for an example of where this change would be specifically useful.

@atinux atinux closed this Sep 6, 2024
@atinux
Copy link
Collaborator

atinux commented Sep 6, 2024

Please take a look at https://github.com/unjs/h3/blob/2d941d3cfb1dddf543d48abe23d13488c88c7432/src/error.ts#L82

the H3Error extends the Error instance so it's exactly the same but it better when dealing with HTTP errors.

@atinux atinux reopened this Sep 6, 2024
@atinux
Copy link
Collaborator

atinux commented Sep 6, 2024

Re-opening as I believe it might be best to use throw createError(error) to forward all properties from the incoming Supabase error

@philliphartin
Copy link
Author

philliphartin commented Sep 9, 2024

Re-opening as I believe it might be best to use throw createError(error) to forward all properties from the incoming Supabase error

That seems like a good idea. Would you like me to make those changes and commit back to the PR?

@larbish
Copy link
Collaborator

larbish commented Sep 10, 2024

@philliphartin I think you can keep this one and rename it.

@philliphartin philliphartin changed the title Removed opinionated createError usage in server utilities, using direct throw instead Directly throw Supabase error within server utilities Sep 10, 2024
@philliphartin philliphartin changed the title Directly throw Supabase error within server utilities Directly throw Supabase error in server utility functions Sep 10, 2024
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.

4 participants