UNPKG

mattermost-redux

Version:

Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client

92 lines (91 loc) 3.32 kB
"use strict"; // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. Object.defineProperty(exports, "__esModule", { value: true }); exports.LogErrorBarMode = void 0; exports.dismissError = dismissError; exports.getLogErrorAction = getLogErrorAction; exports.logError = logError; exports.shouldShowErrorBar = shouldShowErrorBar; exports.clearErrors = clearErrors; const serialize_error_1 = require("serialize-error"); const client4_1 = require("@mattermost/types/client4"); const action_types_1 = require("mattermost-redux/action_types"); const client_1 = require("mattermost-redux/client"); function dismissError(index) { return { type: action_types_1.ErrorTypes.DISMISS_ERROR, index, data: null, }; } function getLogErrorAction(error, displayable = false) { return { type: action_types_1.ErrorTypes.LOG_ERROR, displayable, error, data: null, }; } var LogErrorBarMode; (function (LogErrorBarMode) { /** * Always show the error bar for this error. */ LogErrorBarMode["Always"] = "Always"; /** * Never show the error bar for this error. */ LogErrorBarMode["Never"] = "Never"; /** * Only shows the error bar if Developer Mode is enabled, and the message displayed will tell the user to check the * JS console for more information. */ LogErrorBarMode["InDevMode"] = "InDevMode"; })(LogErrorBarMode || (exports.LogErrorBarMode = LogErrorBarMode = {})); function logError(error, options = {}) { return async (dispatch, getState) => { if (error.server_error_id === 'api.context.session_expired.app_error') { return { data: true }; } let sendToServer = true; const message = error.stack || ''; if (message.includes('TypeError: Failed to fetch')) { sendToServer = false; } if (error.server_error_id) { sendToServer = false; } const serializedError = (0, serialize_error_1.serializeError)(error); if (sendToServer) { try { const stringifiedSerializedError = JSON.stringify(serializedError).toString(); await client_1.Client4.logClientError(stringifiedSerializedError, client4_1.LogLevel.Debug); } catch (err) { // avoid crashing the app if an error sending "the error" occurs. } } if (options && options.errorBarMode === LogErrorBarMode.InDevMode) { serializedError.message = 'A JavaScript error has occurred. Please use the JavaScript console to capture and report the error'; } dispatch(getLogErrorAction(serializedError, shouldShowErrorBar(getState(), options))); return { data: true }; }; } function shouldShowErrorBar(state, options) { if (options && options.errorBarMode === LogErrorBarMode.Always) { return true; } if (options && options.errorBarMode === LogErrorBarMode.InDevMode) { const isDevMode = state.entities.general.config?.EnableDeveloper === 'true'; return isDevMode; } return false; } function clearErrors() { return { type: action_types_1.ErrorTypes.CLEAR_ERRORS, data: null, }; }