Skip to content

Commit

Permalink
feat(gatsby-source-wordpress): Improve 50* error messages (#29972)
Browse files Browse the repository at this point in the history
Co-authored-by: gatsbybot <mathews.kyle+gatsbybot@gmail.com>
  • Loading branch information
TylerBarnes and gatsbybot committed Mar 5, 2021
1 parent 0d1c17d commit a7b1062
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 52 deletions.
101 changes: 101 additions & 0 deletions packages/gatsby-source-wordpress/__tests__/fetch-graphql.test.js
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()
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ const areRemotePluginVersionsSatisfied = async ({
},
panicOnError: false,
throwGqlErrors: true,
isFirstRequest: true,
})

wpgqlIsSatisfied = data.wpGatsbyCompatibility.satisfies.wpGQL
Expand Down Expand Up @@ -262,6 +263,7 @@ const prettyPermalinksAreEnabled = async ({
}
`,
throwGqlErrors: true,
isFirstRequest: true,
})

if (!data.wpGatsby.arePrettyPermalinksEnabled) {
Expand Down

0 comments on commit a7b1062

Please sign in to comment.