From 08bf3a289fc1ceb0613d4cb09c0e2c4c6097833a Mon Sep 17 00:00:00 2001 From: srgooglo Date: Fri, 9 Oct 2020 13:01:13 +0200 Subject: [PATCH] better error handler --- .vscode/functions_schemas.code-snippets | 2 +- config/handlers/errToFlag.js | 9 ++ config/handlers/errToStrings.js | 9 ++ config/handlers/numToError.js | 9 ++ src/core/index.js | 6 +- src/core/libs/errorhandler/index.js | 99 ++++++++++++-------- src/core/libs/style/index.js | 6 +- src/pages/settings/components/theme/index.js | 6 +- 8 files changed, 98 insertions(+), 48 deletions(-) create mode 100644 config/handlers/errToFlag.js create mode 100644 config/handlers/errToStrings.js create mode 100644 config/handlers/numToError.js diff --git a/.vscode/functions_schemas.code-snippets b/.vscode/functions_schemas.code-snippets index 6f2fcd07..3b956c1e 100644 --- a/.vscode/functions_schemas.code-snippets +++ b/.vscode/functions_schemas.code-snippets @@ -5,7 +5,7 @@ "description": "Create a new functional core module with default schema for ycore-schema21", "body": [ "import verbosity from 'core/libs/verbosity'", - "import { onError } from 'core/libs/errorhandler'", + "import handle from 'core/libs/errorhandler'", "import { notify } from 'core/libs/appInterface'", "import settings from 'core/libs/settings'", "import endpoints from 'config/endpoints'", diff --git a/config/handlers/errToFlag.js b/config/handlers/errToFlag.js new file mode 100644 index 00000000..5309a32d --- /dev/null +++ b/config/handlers/errToFlag.js @@ -0,0 +1,9 @@ +module.exports = { + SESSION_INVALID: "DISRUPT", + OVERLAY_BADPOSITION: "IGNORE", + INTERNAL_PROCESS_FAILED: "CRITICAL", + INVALID_DATA: "DISRUPT", + INVALID_PROPS: "IGNORE", + MISSING_REQUIRED_PAYLOAD: "IGNORE" +} + \ No newline at end of file diff --git a/config/handlers/errToStrings.js b/config/handlers/errToStrings.js new file mode 100644 index 00000000..64cef049 --- /dev/null +++ b/config/handlers/errToStrings.js @@ -0,0 +1,9 @@ +module.exports = { + SESSION_INVALID: `Your session is invalid`, + OVERLAY_BADPOSITION: `Invalid overlay position! Was expected "primary" or "secondary"`, + INTERNAL_PROCESS_FAILED: `An internal error has occurred! `, + INVALID_DATA: `A function has been executed with invalid data and has caused an error!`, + INVALID_PROPS: `Some props failed!`, + MISSING_REQUIRED_PAYLOAD: "Error has occurred because the function needed data that does not exist" +} + \ No newline at end of file diff --git a/config/handlers/numToError.js b/config/handlers/numToError.js new file mode 100644 index 00000000..e29bddce --- /dev/null +++ b/config/handlers/numToError.js @@ -0,0 +1,9 @@ +module.exports = { + 400: "SESSION_INVALID", + 210: "OVERLAY_BADPOSITION", + 110: "INTERNAL_PROCESS_FAILED", + 120: "INVALID_DATA", + 130: "INVALID_PROPS", + 140: "MISSING_REQUIRED_PAYLOAD" +} + \ No newline at end of file diff --git a/src/core/index.js b/src/core/index.js index 6de7cee9..fb66fc93 100644 --- a/src/core/index.js +++ b/src/core/index.js @@ -3,7 +3,7 @@ import { format } from 'timeago.js'; import { cloneDeep } from 'lodash'; import store from 'store'; import { i18n, app_config } from 'config'; -import * as errorHandlers from 'core/libs/errorhandler' +import handle from 'core/libs/errorhandler' import platform from 'platform' import request from 'request' import html2canvas from 'html2canvas' @@ -173,7 +173,7 @@ export function downloadDecodedURI(payload){ tmp.download= filename tmp.click() } catch (error) { - errorHandlers.onError.internal_proccess(error) + handle({ msg: error, code: 120 }) } } @@ -194,7 +194,7 @@ export function downloadEncodedURI(payload){ tmp.download= filename tmp.click() } catch (error) { - errorHandlers.onError.internal_proccess(error) + handle({ msg: error, code: 120 }) } } diff --git a/src/core/libs/errorhandler/index.js b/src/core/libs/errorhandler/index.js index f9cbb33a..1725884c 100644 --- a/src/core/libs/errorhandler/index.js +++ b/src/core/libs/errorhandler/index.js @@ -1,43 +1,66 @@ import { appInterface } from 'core/libs' import verbosity from 'core/libs/verbosity' +import errStrings from 'config/handlers/errToStrings.js' +import errNumbers from 'config/handlers/numToError.js' +import errFlags from 'config/handlers/errToFlag.js' -// STRINGS -export const OVERLAY_BADPOSITION = `Invalid overlay position! Was expected "primary" or "secondary"` -export const INTERNAL_PROCESS_FAILED = `An internal error has occurred! ` -export const INVALID_DATA = `A function has been executed with invalid data and has caused an error!` -export const INVALID_PROPS = `Some props failed!` -// HANDLERS -export const onError = { - internal_proccess: (...rest) => { - verbosity({...rest}, { - type: "error" - }) - appInterface.notify.open({ - message: INTERNAL_PROCESS_FAILED, - description: -
-
{JSON.stringify(...rest)}
-
, - - }) - return false - }, - invalid_data: (error, expecting) => { - verbosity({error}, { - type: "error" - }) - appInterface.notify.open({ - message: 'Invalid Data', - description: -
-
{INVALID_DATA}
-
-

Expected: {expecting}

-

{`${error}`}

-
-
, - - }) +export function ErrorHandler(payload, callback){ + if (!payload) { return false } -} \ No newline at end of file + + const flagToString = { + CRITICAL: "An critical exception", + DISRUPT: "An wild error appears!", + IGNORE: "Warning" + } + + const flags = ["CRITICAL", "DISRUPT", "IGNORE"] + let flag = null + let out = null + + const { msg, outFlag, code } = payload + + if (!out && code != null) { // This give priority to resolve with `code` than `outFlag` + out = errNumbers[code] + } + + if (!out && outFlag != null ) { + out = outFlag + } + + if (out && typeof(errStrings[out]) !== "undefined") { + verbosity(msg, {type: "error"}) + flag = errFlags[out] + }else{ + console.log("(Aborted) no out key | or invalid flag => ", out) + return false + } + + appInterface.notify.open({ + message: flagToString[flag] ?? "Unexpected Error", + description: +
+
{msg ?? "No exception message"}
+
=> {errStrings[out] ?? "Unhandled Exception"} | { out?? "UNDEFINED_KEY" }
+
, + }) + + switch (flag) { + case flags[0]: + console.log("FLAG => ", flags[0]) + return false + case flags[1]: + console.log("FLAG => ", flags[1]) + return false + case flags[2]: + console.log("FLAG => ", flags[2]) + return false + default: + console.log('Invalid FLAG') + break; + } +} + + +export default ErrorHandler \ No newline at end of file diff --git a/src/core/libs/style/index.js b/src/core/libs/style/index.js index f27db527..6dc13ce6 100644 --- a/src/core/libs/style/index.js +++ b/src/core/libs/style/index.js @@ -1,7 +1,7 @@ import store from 'store'; import { app_config } from 'config'; import verbosity from 'core/libs/verbosity' -import * as errorHandlers from 'core/libs/errorhandler' +import ErrorHandler from 'core/libs/errorhandler' const { appTheme_desiredContrast, appTheme_container } = app_config @@ -13,7 +13,7 @@ export const theme = { try { raw.forEach((e)=>{container[e.key] = e.value}) } catch (error) { - return errorHandlers.onError.invalid_data(error, "ThemeScheme") + return ErrorHandler({ msg: error, code: 120 }) } return container }, @@ -104,7 +104,7 @@ export function getImagePixelColorsUsingCanvas(canvas, image) { try { imagePixelColors = ctx.getImageData(...destinationCanvasCoordinates); } catch (error) { - return errorHandlers.onError.internal_proccess(error) + return ErrorHandler({ msg: error, code: 120 }) } if (imagePixelColors) { diff --git a/src/pages/settings/components/theme/index.js b/src/pages/settings/components/theme/index.js index 0f862516..5a2c9532 100644 --- a/src/pages/settings/components/theme/index.js +++ b/src/pages/settings/components/theme/index.js @@ -4,7 +4,7 @@ import * as antd from 'antd' import {connect} from 'umi' import styles from './index.less' -import { onError } from 'core/libs/errorhandler' +import ErrorHandler from 'core/libs/errorhandler' import { theme, getOptimalOpacityFromIMG, get_style_rule_value } from 'core/libs/style' import { urlToBase64, imageToBase64, arrayToObject } from 'core' import exportDataAsFile from 'core/libs/appInterface/export_data' @@ -19,7 +19,7 @@ class ThemeConfigurator extends React.Component{ applyStoraged(){ const storaged = theme.get() if(storaged && this.state){ - storaged[this.state.key]? this.setState({ model: storaged[this.state.key]}) : onError.internal_proccess(`"Config key" or "Dispatcher" is missing`) + storaged[this.state.key]? this.setState({ model: storaged[this.state.key]}) : ErrorHandler({ msg: `"Config key" or "Dispatcher" is missing`, code: 140 }) } } @@ -27,7 +27,7 @@ class ThemeConfigurator extends React.Component{ handleUpdate(payload){ if(!this.state.key || !this.props.dispatch) { - return onError.internal_proccess(`"Config key" or "App/Dispatcher" is missing`) + return ErrorHandler({ msg: `"Config key" or "App/Dispatcher" is missing`, code: 140 }) } if (!payload) { payload = this.state.model