Skip to content
This repository has been archived by the owner on Jan 13, 2023. It is now read-only.

Commit

Permalink
fix(api): Add 404 error translation (#141) by @bryanstearns
Browse files Browse the repository at this point in the history
  • Loading branch information
bryanstearns authored and jamonholmgren committed Jan 15, 2019
1 parent e3844cf commit 0369a23
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 4 deletions.
72 changes: 72 additions & 0 deletions boilerplate/src/services/api/api-problem.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { getGeneralApiProblem } from "./api-problem"
import { ApiErrorResponse } from "apisauce"

test("handles connection errors", () => {
expect(getGeneralApiProblem({ problem: "CONNECTION_ERROR" } as ApiErrorResponse<null>)).toEqual({
kind: "cannot-connect",
temporary: true,
})
})

test("handles network errors", () => {
expect(getGeneralApiProblem({ problem: "NETWORK_ERROR" } as ApiErrorResponse<null>)).toEqual({
kind: "cannot-connect",
temporary: true,
})
})

test("handles timeouts", () => {
expect(getGeneralApiProblem({ problem: "TIMEOUT_ERROR" } as ApiErrorResponse<null>)).toEqual({
kind: "timeout",
temporary: true,
})
})

test("handles server errors", () => {
expect(getGeneralApiProblem({ problem: "SERVER_ERROR" } as ApiErrorResponse<null>)).toEqual({
kind: "server",
})
})

test("handles unknown errors", () => {
expect(getGeneralApiProblem({ problem: "UNKNOWN_ERROR" } as ApiErrorResponse<null>)).toEqual({
kind: "unknown",
temporary: true,
})
})

test("handles unauthorized errors", () => {
expect(
getGeneralApiProblem({ problem: "CLIENT_ERROR", status: 401 } as ApiErrorResponse<null>),
).toEqual({
kind: "unauthorized",
})
})

test("handles forbidden errors", () => {
expect(
getGeneralApiProblem({ problem: "CLIENT_ERROR", status: 403 } as ApiErrorResponse<null>),
).toEqual({
kind: "forbidden",
})
})

test("handles not-found errors", () => {
expect(
getGeneralApiProblem({ problem: "CLIENT_ERROR", status: 404 } as ApiErrorResponse<null>),
).toEqual({
kind: "not-found",
})
})

test("handles other client errors", () => {
expect(
getGeneralApiProblem({ problem: "CLIENT_ERROR", status: 418 } as ApiErrorResponse<null>),
).toEqual({
kind: "rejected",
})
})

test("handles cancellation errors", () => {
expect(getGeneralApiProblem({ problem: "CANCEL_ERROR" } as ApiErrorResponse<null>)).toBeNull()
})
2 changes: 2 additions & 0 deletions boilerplate/src/services/api/api-problem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ export function getGeneralApiProblem(response: ApiResponse<any>): GeneralApiProb
return { kind: "unauthorized" }
case 403:
return { kind: "forbidden" }
case 404:
return { kind: "not-found" }
default:
return { kind: "rejected" }
}
Expand Down
16 changes: 12 additions & 4 deletions test/generators-integration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ const BOILERPLATE = `${__dirname}/../`
// calling the ignite cli takes a while
jasmine.DEFAULT_TIMEOUT_INTERVAL = 800000

describe('with a linter', () => {
describe('a generated app', () => {
// creates a new temp directory
const linterTemp = tempy.directory()
const appTemp = tempy.directory()
beforeAll(async () => {
// make sure we are in the temp directory
process.chdir(linterTemp)
process.chdir(appTemp)
await execa(IGNITE, ['new', APP, '--skip-git', '--boilerplate', BOILERPLATE])
process.chdir(APP)
})

afterAll(() => {
// clean up generated test app
jetpack.remove(linterTemp)
jetpack.remove(appTemp)
})

test('can yarn install and pass tests', async () => {
return execa.shell("yarn install 2>&1")
.then(() => execa.shell("npm test 2>&1"))
.catch(error => {
expect(error.stdout).toEqual('') // will fail & show the yarn or test errors
})
})

test('does have a linting script', async () => {
Expand Down

0 comments on commit 0369a23

Please sign in to comment.