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: add option to OrGuard to throw the last or custom error #44

Merged
merged 3 commits into from
Dec 10, 2024

Conversation

p-dim-popov
Copy link
Contributor

No description provided.

Copy link

changeset-bot bot commented May 29, 2024

🦋 Changeset detected

Latest commit: 458f98f

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@nest-lab/or-guard Minor

Not sure what this means? Click here to learn what changesets are.

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

@p-dim-popov p-dim-popov force-pushed the feat/propagate-thrown-error branch from 2b41c5e to 887c6ce Compare May 29, 2024 14:31
@jmcdo29
Copy link
Owner

jmcdo29 commented May 29, 2024

What's the use case for throwing the last encountered error? The first is for a fast fail. What about any errors between the first and last, should they be merged and thrown for better reporting?

@p-dim-popov
Copy link
Contributor Author

p-dim-popov commented May 29, 2024

I have this case

@UseGuards(OrGuard([GuardForUser, GuardForApiKey]))

Both guards when used separately throw 401, but if they fail with OrGuard the result is 403. The thing is that I still need them to throw 401 even when used together. Merging errors is a bit gray area as in theory everything can be thrown and it's hard to say how merging will be done (messages, stack traces, type of the new error, etc...). The simplest solution for me is to throw whatever fails last but it can be better to also expose a way to provide either a custom error or errors narrowing function

@UseGuards(OrGuard([GuardForUser, GuardForApiKey], { throwError: new UnauthorizedException('No authorization provided') }))

Let me propose this override as well 02b7d08

@p-dim-popov p-dim-popov force-pushed the feat/propagate-thrown-error branch 2 times, most recently from b0ae573 to c694b39 Compare May 29, 2024 17:06
@p-dim-popov p-dim-popov changed the title feat: add option to OrGuard to throw the last error feat: add option to OrGuard to throw the last or custom error Jun 13, 2024
@ls-mmacdonald
Copy link

I'm interested in this feature for the same reason (being able to return 401 status code when multiple auth strategies fail rather than 403).

@jmcdo29 Any chance this could be merged? 🙏

@jmcdo29
Copy link
Owner

jmcdo29 commented Dec 10, 2024

Thanks for the ping on this @ls-mmacdonald. This does look like something that's well thought out and could be easily supported. I'll try to make a changeset for this here soon so that it can be merged and published, unless @p-dim-popov wants to get that done, but as it's been a bit I wouldn't expect any quick response.

@jmcdo29 jmcdo29 force-pushed the feat/propagate-thrown-error branch from c77fcd5 to 458f98f Compare December 10, 2024 18:37
@jmcdo29
Copy link
Owner

jmcdo29 commented Dec 10, 2024

Tests pass locally. Merging to main

@jmcdo29 jmcdo29 merged commit 9e49b0b into jmcdo29:main Dec 10, 2024
1 of 5 checks passed
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.

3 participants