mattermost-mobile/app/mm-redux/actions/errors.ts
Miguel Alatzar 2d81b497cf [MM-23520] Port mattermost-redux (#4088)
* Remove mattermost-redux

* Move mm-redux files into app/redux

* Add @redux path to tsconfig.json

* Fix imports

* Install missing dependencies

* Fix tsc errors

* Fix i18n_utils test

* Fix more imports

* Remove redux websocket

* Fix tests

* Rename @redux

* Apply changes from mattermost-redux PR 1103

* Remove mattermost-redux mention in template

* Add missing imports

* Rename app/redux/ to app/mm-redux/

* Remove test file

* Fix fetching Sidebar GM profiles

Co-authored-by: Elias Nahum <nahumhbl@gmail.com>
2020-04-17 21:12:09 -07:00

73 lines
2.2 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {ErrorTypes} from '@mm-redux/action_types';
import {serializeError, ErrorObject} from 'serialize-error';
import {Client4} from '@mm-redux/client';
import EventEmitter from '@mm-redux/utils/event_emitter';
import {DispatchFunc, ActionFunc} from '@mm-redux/types/actions';
import {Error} from '@mm-redux/types/errors';
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};
};
}