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(next-drupal): translatePath, getResourceByPath - throw error when backend error #739

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 28 additions & 4 deletions packages/next-drupal/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,19 @@ export class DrupalClient {
withAuth: options.withAuth,
})

// Server error. But 404 is treated implicitely below.
if (!response?.ok && response.status !== 404) {
let errorMessage: string
try {
const responseJson = await response.json()
errorMessage = `${response.status} ${responseJson?.message}`
} catch (e) {
/* c8 ignore next 2 */
errorMessage = `${response.status} ${response.statusText}`
}
throw new Error(errorMessage)
}

const json = await response.json()

if (!json?.["resolvedResource#uri{0}"]?.body) {
Expand Down Expand Up @@ -890,10 +903,21 @@ export class DrupalClient {
})

if (!response?.ok) {
// Do not throw errors here.
// Otherwise next.js will catch error and throw a 500.
// We want a 404.
return null
// Do not throw errors here when response is 404.
if (response.status === 404) {
return null
}

// Throw error in any other situation as response is not ok.
let errorMessage: string
try {
const responseJson = await response.json()
errorMessage = `${response.status} ${responseJson?.message}`
} catch (e) {
/* c8 ignore next 2 */
errorMessage = `${response.status} ${response.statusText}`
}
throw new Error(errorMessage)
}

const json = await response.json()
Expand Down
26 changes: 26 additions & 0 deletions packages/next-drupal/tests/DrupalClient/resource-methods.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,19 @@ describe("getResourceByPath()", () => {
).rejects.toThrow("Unable to resolve path /path-do-not-exist.")
})

test("throws an error for server errors", async () => {
const client = new DrupalClient(BASE_URL)

spyOnFetch({
responseBody: { message: "mocked internal server error" },
status: 500,
})

await expect(
client.getResourceByPath<DrupalNode>("/server-error")
).rejects.toThrow("500 mocked internal server error")
})

test("throws an error for invalid params", async () => {
const client = new DrupalClient(BASE_URL)

Expand Down Expand Up @@ -836,6 +849,19 @@ describe("translatePath()", () => {
expect(path).toBeNull()
})

test("throws an error for server errors", async () => {
const client = new DrupalClient(BASE_URL)

spyOnFetch({
responseBody: { message: "mocked internal server error" },
status: 500,
})

await expect(client.translatePath("/server-error")).rejects.toThrowError(
"500 mocked internal server error"
)
})

test("makes un-authenticated requests by default", async () => {
const client = new DrupalClient(BASE_URL)
const fetchSpy = jest.spyOn(client, "fetch")
Expand Down
Loading