Commit a4733511 authored by Shpuld Shpludson's avatar Shpuld Shpludson

Merge branch 'feat/notFoundError' into 'master'

404, 403 errors processing

See merge request !52
parents 50946ac1 6cbe779b
Pipeline #31709 passed with stages
in 7 minutes and 24 seconds
......@@ -25,7 +25,7 @@ const authHeaders = ({ accessToken }) => {
}
}
const request = async ({ method = 'GET', url, params, queries, config, fullUrl = undefined, body = undefined, headers = {} }) => {
const request = async ({ method = 'GET', url, queries, config, fullUrl = undefined, body = undefined, headers = {} }) => {
const instance = config.instance
fullUrl = fullUrl || `${instance}${url}`
......@@ -58,7 +58,8 @@ const request = async ({ method = 'GET', url, params, queries, config, fullUrl =
} else {
return {
state: 'error',
data: parsedResult
data: parsedResult,
status: result.status
}
}
} catch (e) {
......
import find from 'lodash/find'
function NotFoundError (message) {
this.name = 'NotFoundError'
this.message = message || 'Not found'
this.stack = (new Error()).stack
}
NotFoundError.prototype = Object.create(Error.prototype)
NotFoundError.prototype.constructor = NotFoundError
function ForbiddenError (message) {
this.name = 'ForbiddenError'
this.message = message || 'Not allowed'
this.stack = (new Error()).stack
}
ForbiddenError.prototype = Object.create(Error.prototype)
ForbiddenError.prototype.constructor = ForbiddenError
export const getConfig = (getState, config) => {
if (!config) {
const { api } = getState()
......@@ -14,14 +30,25 @@ export const apiErrorCatcher = (result) => {
return result
} else {
const errorRes = find(result, ({ state }) => state !== 'ok')
throw new Error((errorRes.data && errorRes.data.error) || errorRes.state)
const error = (errorRes.data && errorRes.data.error) || errorRes.state
if (errorRes.status === 404) {
throw new NotFoundError(error)
} else if (errorRes.status === 403) {
throw new ForbiddenError(error)
}
throw new Error(error)
}
} else {
if (result.state === 'ok') {
return result
} else {
throw new Error((result.data && result.data.error) || result.state)
const error = (result.data && result.data.error) || result.state
if (result.status === 404) {
throw new NotFoundError(error)
} else if (result.status === 403) {
throw new ForbiddenError(error)
}
throw new Error(error)
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment