-
Notifications
You must be signed in to change notification settings - Fork 176
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
Issue 687: Throw error when backend error in /router/translate-path #688
Issue 687: Throw error when backend error in /router/translate-path #688
Conversation
…o avoid re-generation with 404.
Someone is attempting to deploy a commit to the Chapter Three Team on Vercel. A member of the Team first needs to authorize it. |
…is 50x Until now it was always treated as 404 path not found. This treats different 404 than 50x. BREAKING CHANGE: Might break sites relying on response being the same in 404 than in 50x. Fixes chapter-three#687
packages/next-drupal/src/client.ts
Outdated
@@ -910,6 +910,10 @@ export class DrupalClient { | |||
withAuth: options.withAuth, | |||
}) | |||
|
|||
if (response.status && response.status.toString().startsWith("5")) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this code block inside the if (!response?.ok) {
code block below. We don't need to check this if the response is ok.
No need for response.status &&
since response.status
is going to be a number.
Actually, the only "acceptable" error here is 404 not found, right?
We should check
if (response.status === 404) {
return null
}
And otherwise throw the error. Right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done !! Makes more sense this way, thanks!
packages/next-drupal/src/client.ts
Outdated
@@ -910,6 +910,10 @@ export class DrupalClient { | |||
withAuth: options.withAuth, | |||
}) | |||
|
|||
if (response.status && response.status.toString().startsWith("5")) { | |||
throw new Error(`Server error.`) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not throw an error that contains info about the server error?
Please add status code and the error message from the JSON body.
Something like:
let errorMessage
try {
errorMessage = `${response.status} ${(await response.json())?.message}`
} catch (e) {
errorMessage = `${response.status} ${response.statusText}`
}
throw new Error(errorMessage)
We need to wrap the response.json()
in a try/catch block because a 5xx error may cause Drupal to not respond with JSON.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done !! Would this.throwError
be preferable instead of throw
?
await expect( | ||
client.translatePath("/internal-server-error") | ||
).rejects.toThrowError(`Server error.`) | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just refactored the Jest tests. And the translatePath()
tests are now found in packages/next-drupal/tests/DrupalClient/fetch-related-methods.test.ts
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That was actually resource-methods.test.ts
, right?
jest.spyOn(client, "fetch") | ||
.mockImplementation( | ||
jest.fn(() => Promise.reject(new Error(`Server error.`))) as jest.Mock | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When fetch receives a 500 response, it doesn't throw an error; it simply returns the response with the error information. You've mocked fetch
throwing an error (which can happen if you don't pass the right arguments, etc.); see https://developer.mozilla.org/en-US/docs/Web/API/fetch#exceptions
You need to mock a failing response. Fortunately, I just added a test helper function that makes these easy to do.
spyOnFetch({ responseBody: { message: "mocked internal server error" }, status: 500 })
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah. Sorry. I'm not that familiar with it. Fixed.
But I was unable of adding test code for the catch
part in translatePath
. fetchMockImplementation ensures that is valid json response. Any suggestion or just leave it with the c8 ignore
.
… 7.0.2 (chapter-three#690) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
BREAKING CHANGE: The JsonApiWithAuthOptions type has been renamed to JsonApiWithAuthOption. The JsonApiWithLocaleOptions type is now considered deprecated for DrupalClient usage and can be replaced with the JsonApiOptions type.
…preview Fixes chapter-three#517 chapter-three#495 Co-authored-by: Andy Marquis <[email protected]>
…pter-three#697) Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
The `withAuth` option belongs to the DrupalClient.fetch() method but was mistakenly being passed to the init param of the global fetch and custom fetcher functions.
…te urls BREAKING CHANGE: When Drupal uses the Preview Url and the Revalidate Url, the "slug" search param has been renamed to "path". Fixes chapter-three#718
…ranslate-path-error-vs-404
I just noticed that |
Cleaner flow and throw error with relevant message in DrupalClient.translatePath(). Fix test did not reproduce actual response. BREAKING CHANGE: Might break sites relying on response being the same in 404 than in 50x. Fixes chapter-three#687
…th() Add differentiate between server error and 404 in getResourceByPath as it was done in translatePath. Add a test for it. BREAKING CHANGE: Might break sites relying on response being the same in 404 than in 50x. Fixes chapter-three#687
Add c8 ignore to catch branch. fetchMockImplementation forces the response to be valid json. Could only be tested adding an extra argument to not return json but arbitrary response. BREAKING CHANGE: Might break sites relying on response being the same in 404 than in 50x. Fixes chapter-three#687
I've added this to the MR and also a test for it, but now there is a bit of code duplication:
Should I refactor that in a common function or wejust accept it? |
Looking at this more, I realize that the fix is a lot more complicated then "just update getResourceByPath()". I've taken the code from the PR and added to it to make a new PR. #743 Thanks for reporting this bug and thanks for all your effort in fixing it, @marcorcau! |
to avoid re-generation with 404.
This pull request is for: (mark with an "x")
examples/*
modules/next
packages/next-drupal
starters/basic-starter
starters/graphql-starter
GitHub Issue: #687
Describe your changes
When
/router/translate-path?path=xxx
response is "Internal server error" (starts with "5") throw Error instead of returning the same as if it was 404 Not Found to avoid re-generation with 404.