-
Notifications
You must be signed in to change notification settings - Fork 26.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin-next): rename
no-redirect-in-try-catch
to `no-re…
…direct-in-try-catch-without-rethrow` and update docs
- Loading branch information
Showing
6 changed files
with
78 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
--- | ||
title: No Redirect in Try-Catch Without Rethrow | ||
--- | ||
|
||
> Ensure that when using `redirect` within a try-catch block, the catch block must start with a call to `unstable_rethrow` to ensure proper error propagation. | ||
## Why This Error Occurred | ||
|
||
You attempted to use the `redirect` function within a try-catch block without rethrowing the error using `unstable_rethrow`. When `redirect` is called, it throws a `NEXT_REDIRECT` error internally. If this error is caught without being rethrown, it prevents the redirect from executing as intended and suppresses the error handling. | ||
|
||
## Possible Ways to Fix It | ||
|
||
To ensure proper error handling and that the redirect can proceed as intended, the catch block should start with a call to `unstable_rethrow`. This ensures that Next.js's internal error handling is respected and that the redirect is properly executed. | ||
|
||
If you need to handle other errors, you can still do so after the `unstable_rethrow` call. | ||
|
||
## Example | ||
|
||
### Incorrect Usage: | ||
|
||
```javascript | ||
try { | ||
// some code that might throw an error | ||
redirect('/some-path') | ||
} catch (error) { | ||
// handle error | ||
} | ||
``` | ||
|
||
### Correct Usage: | ||
|
||
```javascript | ||
try { | ||
// some code that might throw an error | ||
redirect('/some-path') | ||
} catch (error) { | ||
unstable_rethrow(error) | ||
// handle other errors if necessary | ||
} | ||
``` | ||
|
||
## Useful Links | ||
|
||
- [Next.js Redirect Documentation](/docs/app/building-your-application/routing/redirecting#redirect-function) | ||
- [Next.js Error Handling](/docs/app/building-your-application/routing/error-handling) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 2 additions & 1 deletion
3
...ext/src/rules/no-redirect-in-try-catch.ts → ...-redirect-in-try-catch-without-rethrow.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters