Skip to content

Commit

Permalink
[fix] Black screen of death (HumanSignal#1496)
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-skriabin authored Sep 21, 2021
1 parent db7591d commit fe31f6d
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions label_studio/frontend/src/providers/ApiProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ const API = new APIProxy(API_CONFIG);
export const ApiContext = createContext();
ApiContext.displayName = 'ApiContext';

let apiLocked = false;

const errorFormatter = (result) => {
const {response} = result;
const isShutdown = String(response?.detail ?? result?.error) === 'Failed to fetch';
Expand All @@ -36,8 +38,9 @@ const handleError = async (response, showModal = true) => {
return;
}

const {isShutdown, ...formattedError} = errorFormatter(result);

if (showModal) {
const {isShutdown, ...formattedError} = errorFormatter(result);

modal({
allowClose: !isShutdown,
Expand All @@ -54,22 +57,35 @@ const handleError = async (response, showModal = true) => {
style: { width: 680 },
});
}

return isShutdown;
};

export const ApiProvider = forwardRef(({children}, ref) => {
const [error, setError] = useState(null);

const callApi = useCallback(async (method, { params = {}, errorFilter, ...rest } = {}) => {
if (apiLocked) return;

setError(null);

const result = await API[method](params, rest);

if (result.status === 401) {
apiLocked = true;
location.href = absoluteURL("/");
return;
}

if (result.error) {
const shouldCatchError = errorFilter?.(result) === false;

if (!errorFilter || shouldCatchError){
setError(result);
handleError(result, contextValue.showModal);
const isShutdown = handleError(result, contextValue.showModal);

apiLocked = apiLocked || isShutdown;

return null;
}
}
Expand Down

0 comments on commit fe31f6d

Please sign in to comment.