From 6c06352de2a57918ac6ff15d179e9308aef4d6ea Mon Sep 17 00:00:00 2001 From: Philipp Czora Date: Mon, 4 Mar 2019 11:49:12 +0100 Subject: [PATCH] Added ui error handling for 403 errors --- .../packages/ui-components/src/ErrorNotification.js | 12 ++++++++++-- .../packages/ui-components/src/ErrorPage.js | 4 ++-- .../packages/ui-components/src/apiclient.js | 5 ++++- .../packages/ui-components/src/errors.js | 8 ++++++++ scm-ui/public/locales/de/commons.json | 3 ++- scm-ui/public/locales/en/commons.json | 3 ++- 6 files changed, 28 insertions(+), 7 deletions(-) diff --git a/scm-ui-components/packages/ui-components/src/ErrorNotification.js b/scm-ui-components/packages/ui-components/src/ErrorNotification.js index 380ea04082..b8acf89733 100644 --- a/scm-ui-components/packages/ui-components/src/ErrorNotification.js +++ b/scm-ui-components/packages/ui-components/src/ErrorNotification.js @@ -1,7 +1,7 @@ //@flow import React from "react"; import { translate } from "react-i18next"; -import { BackendError, UnauthorizedError } from "./errors"; +import { BackendError, ForbiddenError, UnauthorizedError } from "./errors"; import Notification from "./Notification"; import BackendErrorNotification from "./BackendErrorNotification"; @@ -27,7 +27,15 @@ class ErrorNotification extends React.Component { ); - } else { + } else if (error instanceof ForbiddenError) { + return ( + + {t("error-notification.prefix")}:{" "} + {t("error-notification.forbidden")} + + ) + } else + { return ( {t("error-notification.prefix")}: {error.message} diff --git a/scm-ui-components/packages/ui-components/src/ErrorPage.js b/scm-ui-components/packages/ui-components/src/ErrorPage.js index 8947e702c8..b86f374263 100644 --- a/scm-ui-components/packages/ui-components/src/ErrorPage.js +++ b/scm-ui-components/packages/ui-components/src/ErrorPage.js @@ -1,7 +1,7 @@ //@flow import React from "react"; import ErrorNotification from "./ErrorNotification"; -import { BackendError } from "./errors"; +import { BackendError, ForbiddenError } from "./errors"; type Props = { error: Error, @@ -26,7 +26,7 @@ class ErrorPage extends React.Component { renderSubtitle = () => { const { error, subtitle } = this.props; - if (error instanceof BackendError) { + if (error instanceof BackendError || error instanceof ForbiddenError) { return null; } return

{subtitle}

diff --git a/scm-ui-components/packages/ui-components/src/apiclient.js b/scm-ui-components/packages/ui-components/src/apiclient.js index ce7de1b757..77ebe8cf4e 100644 --- a/scm-ui-components/packages/ui-components/src/apiclient.js +++ b/scm-ui-components/packages/ui-components/src/apiclient.js @@ -1,6 +1,6 @@ // @flow import { contextPath } from "./urls"; -import {createBackendError, isBackendError, UnauthorizedError} from "./errors"; +import { createBackendError, ForbiddenError, isBackendError, UnauthorizedError } from "./errors"; import type { BackendErrorContent } from "./errors"; const fetchOptions: RequestOptions = { @@ -22,7 +22,10 @@ function handleFailure(response: Response) { } else { if (response.status === 401) { throw new UnauthorizedError("Unauthorized", 401); + } else if (response.status === 403) { + throw new ForbiddenError("Forbidden", 403); } + throw new Error("server returned status code " + response.status); } } diff --git a/scm-ui-components/packages/ui-components/src/errors.js b/scm-ui-components/packages/ui-components/src/errors.js index de425109b4..13e4996cf7 100644 --- a/scm-ui-components/packages/ui-components/src/errors.js +++ b/scm-ui-components/packages/ui-components/src/errors.js @@ -39,6 +39,14 @@ export class UnauthorizedError extends Error { } } +export class ForbiddenError extends Error { + statusCode: number; + constructor(message: string, statusCode: number) { + super(message); + this.statusCode = statusCode; + } +} + export class NotFoundError extends BackendError { constructor(content: BackendErrorContent, statusCode: number) { super(content, "NotFoundError", statusCode); diff --git a/scm-ui/public/locales/de/commons.json b/scm-ui/public/locales/de/commons.json index f32d764ce8..f8007e1b51 100644 --- a/scm-ui/public/locales/de/commons.json +++ b/scm-ui/public/locales/de/commons.json @@ -23,7 +23,8 @@ "prefix": "Fehler", "loginLink": "Erneute Anmeldung", "timeout": "Die Session ist abgelaufen.", - "wrong-login-credentials": "Ungültige Anmeldedaten" + "wrong-login-credentials": "Ungültige Anmeldedaten", + "forbidden": "Sie haben nicht die Berechtigung, diese Entität zu sehen" }, "loading": { "alt": "Lade ..." diff --git a/scm-ui/public/locales/en/commons.json b/scm-ui/public/locales/en/commons.json index fed749a200..cc4edde82c 100644 --- a/scm-ui/public/locales/en/commons.json +++ b/scm-ui/public/locales/en/commons.json @@ -23,7 +23,8 @@ "prefix": "Error", "loginLink": "You can login here again.", "timeout": "The session has expired", - "wrong-login-credentials": "Invalid credentials" + "wrong-login-credentials": "Invalid credentials", + "forbidden": "You don't have permission to view this entity" }, "loading": { "alt": "Loading ..."