-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2281 from botpress/ya-cascading-error
fix(admin): cascading errors and updated typings
- Loading branch information
Showing
6 changed files
with
112 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import axios from 'axios' | ||
import Promise from 'bluebird' | ||
import _ from 'lodash' | ||
|
||
import { toastFailure } from './utils/toaster' | ||
import { getActiveWorkspace, logout, pullToken } from './Auth' | ||
|
||
export const toastError = error => { | ||
const errorCode = _.get(error, 'response.data.errorCode') || _.get(error, 'errorCode') | ||
const details = _.get(error, 'response.data.message') || _.get(error, 'message') | ||
const docs = _.get(error, 'response.data.docs') || _.get(error, 'docs') | ||
|
||
let message = ( | ||
<span> | ||
{errorCode && <span>[{errorCode}]</span>} {details}{' '} | ||
{docs && ( | ||
<a href={docs} target="_blank"> | ||
More informations | ||
</a> | ||
)} | ||
</span> | ||
) | ||
|
||
if (!errorCode && !message) { | ||
message = <span>Something wrong happened. Please try again later.</span> | ||
} | ||
|
||
toastFailure(message) | ||
} | ||
|
||
const createClient = (clientOptions: any, options: { toastErrors?: boolean }) => { | ||
const client = axios.create({ timeout: 2000, ...clientOptions }) | ||
|
||
client.interceptors.response.use( | ||
response => response, | ||
error => { | ||
const wrappedError = _.get(error, 'response.data') | ||
const errorCode = _.get(wrappedError, 'errorCode') | ||
if (errorCode) { | ||
if (['BP_0041'].includes(errorCode)) { | ||
return logout() | ||
} | ||
return Promise.reject(wrappedError) | ||
} else { | ||
return Promise.reject(error) | ||
} | ||
} | ||
) | ||
|
||
if (options.toastErrors) { | ||
client.interceptors.response.use( | ||
response => response, | ||
error => { | ||
toastError(error) | ||
|
||
return Promise.reject(error) | ||
} | ||
) | ||
} | ||
return client | ||
} | ||
|
||
const overrideApiUrl = process.env.REACT_APP_API_URL | ||
? { baseURL: `${process.env.REACT_APP_API_URL}/api/v1` } | ||
: { baseURL: `${window['ROOT_PATH']}/api/v1` } | ||
|
||
export default { | ||
getApiPath() { | ||
return overrideApiUrl.baseURL | ||
}, | ||
|
||
getSecured({ token = undefined, toastErrors = true, timeout = 2000 } = {}) { | ||
if (!token) { | ||
const ls = pullToken() | ||
token = ls && ls.token | ||
} | ||
|
||
return createClient( | ||
{ | ||
timeout, | ||
headers: { | ||
Authorization: `Bearer ${token}`, | ||
'X-BP-Workspace': getActiveWorkspace() | ||
}, | ||
...overrideApiUrl | ||
}, | ||
{ toastErrors } | ||
) | ||
}, | ||
|
||
getAnonymous({ toastErrors = true } = {}) { | ||
return createClient(overrideApiUrl, { toastErrors }) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,17 @@ | ||
import { Position, Toaster } from '@blueprintjs/core' | ||
import { Intent, Position, Toaster } from '@blueprintjs/core' | ||
import _ from 'lodash' | ||
import React from 'react' | ||
|
||
export const AppToaster = Toaster.create({ | ||
className: 'recipe-toaster', | ||
position: Position.TOP | ||
}) | ||
export const toastSuccess = message => | ||
Toaster.create({ className: 'recipe-toaster', position: Position.TOP_RIGHT }).show({ | ||
message, | ||
intent: Intent.SUCCESS, | ||
timeout: 1000 | ||
}) | ||
|
||
export const toastFailure = message => | ||
Toaster.create({ className: 'recipe-toaster', position: Position.TOP_RIGHT }).show({ | ||
message, | ||
intent: Intent.DANGER, | ||
timeout: 3000 | ||
}) |