mattermost-mobile/app/mm-redux/actions/errors.ts
Daniel Espino García 4c8594d330
Add linter rules for import order and type member delimiters (#5514)
* Add linter rules for import order and type member delimiters

* Remove unneeded group

* Group all app/* imports before the internal imports

* Move app/ imports before parent imports

* Separate @node_modules imports into a different group

* Substitute app paths by aliases

* Fix @node_modules import order and add test related modules

* Add aliases for types and test, and group import types
2021-07-23 11:06:04 +02:00

74 lines
2.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {serializeError, ErrorObject} from 'serialize-error';
import {Client4} from '@client/rest';
import {ErrorTypes} from '@mm-redux/action_types';
import {DispatchFunc, ActionFunc} from '@mm-redux/types/actions';
import {Error} from '@mm-redux/types/errors';
import EventEmitter from '@mm-redux/utils/event_emitter';
export function dismissErrorObject(index: number) {
return {
type: ErrorTypes.DISMISS_ERROR,
index,
data: null,
};
}
export function dismissError(index: number): ActionFunc {
return async (dispatch: DispatchFunc) => {
dispatch(dismissErrorObject(index));
return {data: true};
};
}
export function getLogErrorAction(error: ErrorObject, displayable = false) {
return {
type: ErrorTypes.LOG_ERROR,
displayable,
error,
data: null,
};
}
export function logError(error: Error, displayable = false): ActionFunc {
return async (dispatch: DispatchFunc) => {
if (error.server_error_id === 'api.context.session_expired.app_error') {
return {data: true};
}
const serializedError = serializeError(error);
let sendToServer = true;
if (error.stack && error.stack.includes('TypeError: Failed to fetch')) {
sendToServer = false;
}
if (error.server_error_id) {
sendToServer = false;
}
if (sendToServer) {
try {
const stringifiedSerializedError = JSON.stringify(serializedError).toString();
await Client4.logClientError(stringifiedSerializedError);
} catch (err) {
// avoid crashing the app if an error sending
// the error occurs.
}
}
EventEmitter.emit(ErrorTypes.LOG_ERROR, error);
dispatch(getLogErrorAction(serializedError, displayable));
return {data: true};
};
}
export function clearErrors(): ActionFunc {
return async (dispatch: DispatchFunc) => {
dispatch({type: ErrorTypes.CLEAR_ERRORS, data: null});
return {data: true};
};
}