error-logging.js (1102B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 /** 8 * Error logging middleware that will forward all actions that contain an error property 9 * to the console. 10 */ 11 function errorLoggingMiddleware() { 12 return next => action => { 13 if (action.error) { 14 const { error } = action; 15 if (error.message) { 16 console.error(`[ACTION FAILED] ${action.type}: ${error.message}`); 17 } else if (typeof error === "string") { 18 // All failure actions should dispatch an error object instead of a message. 19 // We allow some flexibility to still provide some error logging. 20 console.error(`[ACTION FAILED] ${action.type}: ${error}`); 21 console.error( 22 `[ACTION FAILED] ${action.type} should dispatch the error object!` 23 ); 24 } 25 26 if (error.stack) { 27 console.error(error.stack); 28 } 29 } 30 31 return next(action); 32 }; 33 } 34 35 module.exports = errorLoggingMiddleware;