mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
Fixed 401 message on login
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
// @flow
|
// @flow
|
||||||
import { contextPath } from "./urls";
|
import { contextPath } from "./urls";
|
||||||
import { createBackendError } from "./errors";
|
import {createBackendError, isBackendError, UnauthorizedError} from "./errors";
|
||||||
import type { BackendErrorContent } from "./errors";
|
import type { BackendErrorContent } from "./errors";
|
||||||
|
|
||||||
const fetchOptions: RequestOptions = {
|
const fetchOptions: RequestOptions = {
|
||||||
@@ -10,9 +10,7 @@ const fetchOptions: RequestOptions = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function isBackendError(response) {
|
|
||||||
return response.headers.get("Content-Type") === "application/vnd.scmm-error+json;v=2";
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleFailure(response: Response) {
|
function handleFailure(response: Response) {
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
@@ -22,6 +20,9 @@ function handleFailure(response: Response) {
|
|||||||
throw createBackendError(content, response.status);
|
throw createBackendError(content, response.status);
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
|
if (response.status === 401) {
|
||||||
|
throw new UnauthorizedError("Unauthorized", 401);
|
||||||
|
}
|
||||||
throw new Error("server returned status code " + response.status);
|
throw new Error("server returned status code " + response.status);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
// @flow
|
// @flow
|
||||||
type Context = {type: string, id: string}[];
|
type Context = { type: string, id: string }[];
|
||||||
|
|
||||||
export type BackendErrorContent = {
|
export type BackendErrorContent = {
|
||||||
transactionId: string,
|
transactionId: string,
|
||||||
@@ -10,7 +10,6 @@ export type BackendErrorContent = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
export class BackendError extends Error {
|
export class BackendError extends Error {
|
||||||
|
|
||||||
transactionId: string;
|
transactionId: string;
|
||||||
errorCode: string;
|
errorCode: string;
|
||||||
url: ?string;
|
url: ?string;
|
||||||
@@ -26,12 +25,13 @@ export class BackendError extends Error {
|
|||||||
this.context = content.context;
|
this.context = content.context;
|
||||||
this.statusCode = statusCode;
|
this.statusCode = statusCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class UnauthorizedError extends BackendError {
|
export class UnauthorizedError extends Error {
|
||||||
constructor(content: BackendErrorContent, statusCode: number) {
|
statusCode: number;
|
||||||
super(content, "UnauthorizedError", statusCode);
|
constructor(message: string, statusCode: number) {
|
||||||
|
super(message);
|
||||||
|
this.statusCode = statusCode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -40,14 +40,18 @@ export class NotFoundError extends BackendError {
|
|||||||
super(content, "NotFoundError", statusCode);
|
super(content, "NotFoundError", statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
export function createBackendError(
|
||||||
export function createBackendError(content: BackendErrorContent, statusCode: number) {
|
content: BackendErrorContent,
|
||||||
|
statusCode: number
|
||||||
|
) {
|
||||||
switch (statusCode) {
|
switch (statusCode) {
|
||||||
case 401:
|
|
||||||
return new UnauthorizedError(content, statusCode);
|
|
||||||
case 404:
|
case 404:
|
||||||
return new NotFoundError(content, statusCode);
|
return new NotFoundError(content, statusCode);
|
||||||
default:
|
default:
|
||||||
return new BackendError(content, "BackendError", statusCode);
|
return new BackendError(content, "BackendError", statusCode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function isBackendError(response: Response) {
|
||||||
|
return response.headers.get("Content-Type") === "application/vnd.scmm-error+json;v=2";
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user