better error handler

This commit is contained in:
srgooglo 2020-10-09 13:01:13 +02:00
parent f2ce280264
commit 08bf3a289f
8 changed files with 98 additions and 48 deletions

View File

@ -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'",

View File

@ -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"
}

View File

@ -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"
}

View File

@ -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"
}

View File

@ -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 })
}
}

View File

@ -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:
<div style={{ display: 'flex', flexDirection: 'column', margin: 'auto' }}>
<div style={{ margin: '10px 0' }}> {JSON.stringify(...rest)} </div>
</div>,
})
return false
},
invalid_data: (error, expecting) => {
verbosity({error}, {
type: "error"
})
appInterface.notify.open({
message: 'Invalid Data',
description:
<div style={{ display: 'flex', flexDirection: 'column', margin: 'auto' }}>
<div style={{ margin: '10px 0' }}> {INVALID_DATA} </div>
<div style={{ margin: '10px 0', color: '#333' }}>
<h4>Expected: {expecting}</h4>
<h4 style={{ backgroundColor: 'rgba(221, 42, 42, 0.8)' }} >{`${error}`} </h4>
</div>
</div>,
})
export function ErrorHandler(payload, callback){
if (!payload) {
return false
}
}
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:
<div style={{ display: 'flex', flexDirection: 'column', margin: 'auto', height: "auto" }}>
<div style={{ margin: '10px 0' }}> {msg ?? "No exception message"} </div>
<div> => {errStrings[out] ?? "Unhandled Exception"} | { out?? "UNDEFINED_KEY" } </div>
</div>,
})
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

View File

@ -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) {

View File

@ -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