From e1a0a367b4a60daa186e3777e2d29b6403ef4e77 Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Wed, 14 Nov 2018 21:34:10 +0100 Subject: [PATCH] Fixed handling of server error messages in apiclient --- .../packages/ui-components/package.json | 3 +- .../packages/ui-components/src/apiclient.js | 27 ++++++-- .../ui-components/src/apiclient.test.js | 68 ++++++++++++++++--- .../packages/ui-components/yarn.lock | 23 ++++++- 4 files changed, 106 insertions(+), 15 deletions(-) diff --git a/scm-ui-components/packages/ui-components/package.json b/scm-ui-components/packages/ui-components/package.json index 823ca7143e..4a4b4dc82e 100644 --- a/scm-ui-components/packages/ui-components/package.json +++ b/scm-ui-components/packages/ui-components/package.json @@ -18,6 +18,7 @@ "create-index": "^2.3.0", "enzyme": "^3.5.0", "enzyme-adapter-react-16": "^1.3.1", + "fetch-mock": "^7.2.5", "flow-bin": "^0.79.1", "flow-typed": "^2.5.1", "jest": "^23.5.0", @@ -55,4 +56,4 @@ ] ] } -} \ No newline at end of file +} diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index bd19dcdf14..57e713546c 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -1,5 +1,5 @@ // @flow -import { contextPath } from "./urls"; +import {contextPath} from "./urls"; export const NOT_FOUND_ERROR = Error("not found"); export const UNAUTHORIZED_ERROR = Error("unauthorized"); @@ -11,15 +11,34 @@ const fetchOptions: RequestOptions = { } }; +// TODO: dedup function handleStatusCode(response: Response) { if (!response.ok) { if (response.status === 401) { - throw UNAUTHORIZED_ERROR; + return response.json().then( + json => { + throw Error(json.message); + }, + () => { + throw UNAUTHORIZED_ERROR; + } + ); } if (response.status === 404) { - throw NOT_FOUND_ERROR; + return response.json().then( + json => { + throw Error(json.message); + }, + () => { + throw NOT_FOUND_ERROR; + } + ); } - throw new Error("server returned status code " + response.status); + return response.json().then(json => { + throw Error(json.message); + }, () => { + throw new Error("server returned status code " + response.status); + }); } return response; } diff --git a/scm-ui-components/packages/ui-components/src/apiclient.test.js b/scm-ui-components/packages/ui-components/src/apiclient.test.js index deb22a3b54..bf3358fe95 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.test.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.test.js @@ -1,15 +1,65 @@ // @flow -import { createUrl } from "./apiclient"; +import {apiClient, createUrl} from "./apiclient"; +import fetchMock from "fetch-mock"; -describe("create url", () => { - it("should not change absolute urls", () => { - expect(createUrl("https://www.scm-manager.org")).toBe( - "https://www.scm-manager.org" - ); +describe("apiClient", () => { + afterEach(() => { + fetchMock.reset(); + fetchMock.restore(); }); - it("should add prefix for api", () => { - expect(createUrl("/users")).toBe("/api/v2/users"); - expect(createUrl("users")).toBe("/api/v2/users"); + describe("create url", () => { + it("should not change absolute urls", () => { + expect(createUrl("https://www.scm-manager.org")).toBe( + "https://www.scm-manager.org" + ); + }); + + it("should add prefix for api", () => { + expect(createUrl("/users")).toBe("/api/v2/users"); + expect(createUrl("users")).toBe("/api/v2/users"); + }); + }); + + describe("error handling", () => { + const error = { + message: "Error!!" + }; + + it("should append default error message for 401 if none provided", () => { + fetchMock.mock("api/v2/foo", 401); + return apiClient + .get("foo") + .catch(err => { + expect(err.message).toEqual("unauthorized"); + }); + }); + + it("should append error message for 401 if provided", () => { + fetchMock.mock("api/v2/foo", {"status": 401, body: error}); + return apiClient + .get("foo") + .catch(err => { + expect(err.message).toEqual("Error!!"); + }); + }); + + it("should append default error message for 401 if none provided", () => { + fetchMock.mock("api/v2/foo", 404); + return apiClient + .get("foo") + .catch(err => { + expect(err.message).toEqual("not found"); + }); + }); + + it("should append error message for 404 if provided", () => { + fetchMock.mock("api/v2/foo", {"status": 404, body: error}); + return apiClient + .get("foo") + .catch(err => { + expect(err.message).toEqual("Error!!"); + }); + }); }); }); diff --git a/scm-ui-components/packages/ui-components/yarn.lock b/scm-ui-components/packages/ui-components/yarn.lock index f11cfa5bcd..d7afe51035 100644 --- a/scm-ui-components/packages/ui-components/yarn.lock +++ b/scm-ui-components/packages/ui-components/yarn.lock @@ -688,6 +688,10 @@ react "^16.4.2" react-dom "^16.4.2" +"@scm-manager/ui-types@2.0.0-SNAPSHOT": + version "2.0.0-20181010-130547" + resolved "https://registry.yarnpkg.com/@scm-manager/ui-types/-/ui-types-2.0.0-20181010-130547.tgz#9987b519e43d5c4b895327d012d3fd72429a7953" + "@types/node@*": version "10.12.0" resolved "https://registry.yarnpkg.com/@types/node/-/node-10.12.0.tgz#ea6dcbddbc5b584c83f06c60e82736d8fbb0c235" @@ -2995,6 +2999,15 @@ fb-watchman@^2.0.0: dependencies: bser "^2.0.0" +fetch-mock@^7.2.5: + version "7.2.5" + resolved "https://registry.yarnpkg.com/fetch-mock/-/fetch-mock-7.2.5.tgz#4682f51b9fa74d790e10a471066cb22f3ff84d48" + dependencies: + babel-polyfill "^6.26.0" + glob-to-regexp "^0.4.0" + path-to-regexp "^2.2.1" + whatwg-url "^6.5.0" + figures@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/figures/-/figures-2.0.0.tgz#3ab1a2d2a62c8bfb431a0c94cb797a2fce27c962" @@ -3341,6 +3354,10 @@ glob-stream@^3.1.5: through2 "^0.6.1" unique-stream "^1.0.0" +glob-to-regexp@^0.4.0: + version "0.4.0" + resolved "https://registry.yarnpkg.com/glob-to-regexp/-/glob-to-regexp-0.4.0.tgz#49bd677b1671022bd10921c3788f23cdebf9c7e6" + glob-watcher@^0.0.6: version "0.0.6" resolved "https://registry.yarnpkg.com/glob-watcher/-/glob-watcher-0.0.6.tgz#b95b4a8df74b39c83298b0c05c978b4d9a3b710b" @@ -5982,6 +5999,10 @@ path-to-regexp@^1.7.0: dependencies: isarray "0.0.1" +path-to-regexp@^2.2.1: + version "2.4.0" + resolved "https://registry.yarnpkg.com/path-to-regexp/-/path-to-regexp-2.4.0.tgz#35ce7f333d5616f1c1e1bfe266c3aba2e5b2e704" + path-type@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/path-type/-/path-type-1.1.0.tgz#59c44f7ee491da704da415da5a4070ba4f8fe441" @@ -7814,7 +7835,7 @@ whatwg-mimetype@^2.1.0: version "2.2.0" resolved "https://registry.yarnpkg.com/whatwg-mimetype/-/whatwg-mimetype-2.2.0.tgz#a3d58ef10b76009b042d03e25591ece89b88d171" -whatwg-url@^6.4.1: +whatwg-url@^6.4.1, whatwg-url@^6.5.0: version "6.5.0" resolved "https://registry.yarnpkg.com/whatwg-url/-/whatwg-url-6.5.0.tgz#f2df02bff176fd65070df74ad5ccbb5a199965a8" dependencies: