1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-10-18 20:09:08 +02:00
unleash.unleash/frontend/src/store/api-helper.js
Christopher Kolstad 2cabe7f297 Add tag feature
- CRUD for tag-types
- CD for tags
- tagging for features
- display tags on feature-toggle
2021-01-18 09:26:32 +01:00

78 lines
2.5 KiB
JavaScript

const defaultErrorMessage = 'Unexpected exception when talking to unleash-api';
function extractJoiMsg(body) {
return body.details.length > 0 ? body.details[0].message : defaultErrorMessage;
}
function extractLegacyMsg(body) {
return body && body.length > 0 ? body[0].msg : defaultErrorMessage;
}
class ServiceError extends Error {
constructor(statusCode = 500) {
super(defaultErrorMessage);
this.name = 'ServiceError';
this.statusCode = statusCode;
}
}
export class AuthenticationError extends Error {
constructor(statusCode, body) {
super('Authentication required');
this.name = 'AuthenticationError';
this.statusCode = statusCode;
this.body = body;
}
}
export class ForbiddenError extends Error {
constructor(statusCode, body) {
super('You cannot perform this action');
this.name = 'ForbiddenError';
this.statusCode = statusCode;
this.body = body;
}
}
export class NotFoundError extends Error {
constructor(statusCode) {
super('The requested resource could not be found but may be available in the future');
this.name = 'NotFoundError';
this.statusCode = statusCode;
}
}
export function throwIfNotSuccess(response) {
if (!response.ok) {
if (response.status === 401) {
return new Promise((resolve, reject) => {
response.json().then(body => reject(new AuthenticationError(response.status, body)));
});
} else if (response.status === 403) {
return new Promise((resolve, reject) => {
response.json().then(body => reject(new ForbiddenError(response.status, body)));
});
} else if (response.status === 404) {
return new Promise((resolve, reject) => {
reject(new NotFoundError(response.status));
});
} else if (response.status > 399 && response.status < 499) {
return new Promise((resolve, reject) => {
response.json().then(body => {
const errorMsg = body && body.isJoi ? extractJoiMsg(body) : extractLegacyMsg(body);
let error = new Error(errorMsg);
error.statusCode = response.status;
reject(error);
});
});
} else {
return Promise.reject(new ServiceError(response.status));
}
}
return Promise.resolve(response);
}
export const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
};