mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-10 15:35:49 +01:00
Fixed handling of server error messages in apiclient
This commit is contained in:
@@ -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",
|
||||
|
||||
@@ -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) {
|
||||
return response.json().then(
|
||||
json => {
|
||||
throw Error(json.message);
|
||||
},
|
||||
() => {
|
||||
throw UNAUTHORIZED_ERROR;
|
||||
}
|
||||
);
|
||||
}
|
||||
if (response.status === 404) {
|
||||
return response.json().then(
|
||||
json => {
|
||||
throw Error(json.message);
|
||||
},
|
||||
() => {
|
||||
throw NOT_FOUND_ERROR;
|
||||
}
|
||||
);
|
||||
}
|
||||
return response.json().then(json => {
|
||||
throw Error(json.message);
|
||||
}, () => {
|
||||
throw new Error("server returned status code " + response.status);
|
||||
});
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
// @flow
|
||||
import { createUrl } from "./apiclient";
|
||||
import {apiClient, createUrl} from "./apiclient";
|
||||
import fetchMock from "fetch-mock";
|
||||
|
||||
describe("create url", () => {
|
||||
describe("apiClient", () => {
|
||||
afterEach(() => {
|
||||
fetchMock.reset();
|
||||
fetchMock.restore();
|
||||
});
|
||||
|
||||
describe("create url", () => {
|
||||
it("should not change absolute urls", () => {
|
||||
expect(createUrl("https://www.scm-manager.org")).toBe(
|
||||
"https://www.scm-manager.org"
|
||||
@@ -12,4 +19,47 @@ describe("create url", () => {
|
||||
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!!");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user