-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-source-wordpress): Improve 50* error messages (#29972)
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
- Loading branch information
1 parent
0d1c17d
commit a7b1062
Showing
4 changed files
with
232 additions
and
52 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
packages/gatsby-source-wordpress/__tests__/fetch-graphql.test.js
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,101 @@ | ||
import chalk from "chalk" | ||
import fetchGraphQL, { moduleHelpers } from "../dist/utils/fetch-graphql" | ||
import store from "../dist/store" | ||
|
||
describe(`fetchGraphQL helper`, () => { | ||
let mock | ||
const panicMessages = [] | ||
|
||
beforeAll(() => { | ||
const sharedError = `Request failed with status code` | ||
try { | ||
mock = jest.spyOn(moduleHelpers, `getHttp`).mockImplementation(() => { | ||
return { | ||
post: (_url, { query }) => { | ||
if (typeof query === `number`) { | ||
throw new Error(`${sharedError} ${query}`) | ||
} | ||
|
||
if (query === `wpgraphql-deactivated`) { | ||
return { | ||
request: {}, | ||
headers: { | ||
[`content-type`]: `text/html`, | ||
}, | ||
} | ||
} | ||
|
||
return null | ||
}, | ||
} | ||
}) | ||
} catch (e) { | ||
if (!e.message.includes(sharedError)) { | ||
throw e | ||
} | ||
} | ||
|
||
const fakeReporter = { | ||
panic: ({ context: { sourceMessage } }) => { | ||
panicMessages.push(sourceMessage) | ||
}, | ||
} | ||
|
||
store.dispatch.gatsbyApi.setState({ | ||
helpers: { | ||
reporter: fakeReporter, | ||
}, | ||
}) | ||
}) | ||
|
||
test(`handles 500 errors`, async () => { | ||
await fetchGraphQL({ | ||
query: 500, | ||
url: `fake url`, | ||
}) | ||
|
||
expect( | ||
panicMessages[0].includes( | ||
`Your WordPress server is either overloaded or encountered a PHP error.` | ||
) | ||
).toBeTruthy() | ||
}) | ||
|
||
test(`handles 502, 503, and 504 errors`, async () => { | ||
await fetchGraphQL({ | ||
query: 502, | ||
url: `fake url`, | ||
}) | ||
await fetchGraphQL({ | ||
query: 503, | ||
url: `fake url`, | ||
}) | ||
await fetchGraphQL({ | ||
query: 504, | ||
url: `fake url`, | ||
}) | ||
|
||
const errorMessage = `Your WordPress server at ${chalk.bold( | ||
`fake url` | ||
)} appears to be overloaded.` | ||
|
||
expect(panicMessages[1].includes(errorMessage)).toBeTruthy() | ||
expect(panicMessages[2].includes(errorMessage)).toBeTruthy() | ||
expect(panicMessages[3].includes(errorMessage)).toBeTruthy() | ||
}) | ||
|
||
test(`errors when WPGraphQL is not active`, async () => { | ||
await fetchGraphQL({ | ||
query: `wpgraphql-deactivated`, | ||
url: `fake url`, | ||
}) | ||
|
||
expect( | ||
panicMessages[4].includes(`Unable to connect to WPGraphQL.`) | ||
).toBeTruthy() | ||
}) | ||
|
||
afterAll(() => { | ||
mock.mockRestore() | ||
}) | ||
}) |
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
Oops, something went wrong.