Skip to content

Commit

Permalink
optimize log helper to remove unused error type in prod environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalguptaofficial committed Aug 14, 2022
1 parent 83a4c0d commit 671f66f
Show file tree
Hide file tree
Showing 6 changed files with 168 additions and 110 deletions.
69 changes: 38 additions & 31 deletions src/common/enums.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
export enum ERROR_TYPE {
InvalidUpdateColumn = "invalid_update_column",
UndefinedColumn = "undefined_column",
UndefinedValue = "undefined_value",
UndefinedColumnName = "undefined_column_name",
UndefinedDbName = "undefined_database_name",
UndefinedColumnValue = "undefined_column_value",
NotArray = "not_array",
NoValueSupplied = "no_value_supplied",
ColumnNotExist = "column_not_exist",
EnableSearchOff = "enable_search_off",
InvalidOp = "invalid_operator",
NullValue = "null_value",
WrongDataType = "wrong_data_type",
TableNotExist = "table_not_exist",
DbNotExist = "db_not_exist",
ConnectionAborted = "connection_aborted",
ConnectionClosed = "connection_closed",
NotObject = "not_object",
InvalidConfig = "invalid_config",
DbBlocked = "Db_blocked",
IndexedDbNotSupported = "indexeddb_not_supported",
NullValueInWhere = "null_value_in_where",
InvalidJoinQuery = 'invalid_join_query',
InvalidOrderQuery = 'invalid_order_query',
InvalidQuery = 'invalid_query',
InvalidGroupQuery = 'invalid_group_query',
ImportScriptsFailed = 'import_scripts_failed',
MethodNotExist = 'method_not_exist',
Unknown = "unknown",
InvalidMiddleware = "invalid_middleware"
import { IErrorType } from "./interfaces";

export const ERROR_TYPE = {
InvalidUpdateColumn: "invalid_update_column",
UndefinedColumn: "undefined_column",
UndefinedValue: "undefined_value",
UndefinedColumnName: "undefined_column_name",
UndefinedDbName: "undefined_database_name",
UndefinedColumnValue: "undefined_column_value",
NotArray: "not_array",
NoValueSupplied: "no_value_supplied",
ColumnNotExist: "column_not_exist",
EnableSearchOff: "enable_search_off",
InvalidOp: "invalid_operator",
NullValue: "null_value",
WrongDataType: "wrong_data_type",
TableNotExist: "table_not_exist",
DbNotExist: "db_not_exist",
ConnectionAborted: "connection_aborted",
ConnectionClosed: "connection_closed",
NotObject: "not_object",
InvalidConfig: "invalid_config",
DbBlocked: "Db_blocked",
IndexedDbNotSupported: "indexeddb_not_supported",
NullValueInWhere: "null_value_in_where",
InvalidJoinQuery: 'invalid_join_query',
InvalidQuery: 'invalid_query',
ImportScriptsFailed: 'import_scripts_failed',
MethodNotExist: 'method_not_exist',
Unknown: "unknown",
InvalidMiddleware: "invalid_middleware"
} as IErrorType;

if (process.env.NODE_ENV !== 'production') {
Object.assign(ERROR_TYPE, {
InvalidOrderQuery: 'invalid_order_query',
InvalidGroupQuery: 'invalid_group_query'
})
}

export enum WORKER_STATUS {
Expand Down
37 changes: 35 additions & 2 deletions src/common/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ERROR_TYPE, CONNECTION_STATUS } from "./enums";
import { SqlWebResult } from "./types";

export interface IError {
type: ERROR_TYPE;
type: string;
message: string;
}

Expand Down Expand Up @@ -194,7 +194,7 @@ export interface ISqlWeb {

export interface IDbStatus {
conStatus: CONNECTION_STATUS;
lastError?: ERROR_TYPE;
lastError?: IErrorType;
}

export interface IPlugin {
Expand All @@ -204,4 +204,37 @@ export interface IPlugin {
export interface IDbInfo {
name: string;
version: number;
}

export interface IErrorType {
InvalidUpdateColumn: "invalid_update_column",
UndefinedColumn: "undefined_column",
UndefinedValue: "undefined_value",
UndefinedColumnName: "undefined_column_name",
UndefinedDbName: "undefined_database_name",
UndefinedColumnValue: "undefined_column_value",
NotArray: "not_array",
NoValueSupplied: "no_value_supplied",
ColumnNotExist: "column_not_exist",
EnableSearchOff: "enable_search_off",
InvalidOp: "invalid_operator",
NullValue: "null_value",
WrongDataType: "wrong_data_type",
TableNotExist: "table_not_exist",
DbNotExist: "db_not_exist",
ConnectionAborted: "connection_aborted",
ConnectionClosed: "connection_closed",
NotObject: "not_object",
InvalidConfig: "invalid_config",
DbBlocked: "Db_blocked",
IndexedDbNotSupported: "indexeddb_not_supported",
NullValueInWhere: "null_value_in_where",
InvalidJoinQuery: 'invalid_join_query',
InvalidOrderQuery: 'invalid_order_query',
InvalidQuery: 'invalid_query',
InvalidGroupQuery: 'invalid_group_query',
ImportScriptsFailed: 'import_scripts_failed',
MethodNotExist: 'method_not_exist',
Unknown: "unknown",
InvalidMiddleware: "invalid_middleware"
}
5 changes: 2 additions & 3 deletions src/main/log_helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
import { ERROR_TYPE, IError } from "../common/index";

export class LogHelper implements IError {
type: ERROR_TYPE;
name: ERROR_TYPE;
type: string;
message: string;
private _info: any;
status: boolean;

constructor(type: ERROR_TYPE, info?) {
constructor(type: string, info?) {
this.type = type;
this._info = info;
this.message = this.getMsg();
Expand Down
2 changes: 1 addition & 1 deletion src/worker/executors/insert/values_checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class ValuesChecker {
}
}

private getError(error: ERROR_TYPE, details: object) {
private getError(error: string, details: object) {
return new LogHelper(error, details);
}
}
4 changes: 2 additions & 2 deletions src/worker/executors/select/join.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Select } from "./";
import { IJoinQuery, DATA_TYPE, ERROR_TYPE, ISelectQuery } from "@/common";
import { IJoinQuery, DATA_TYPE, ERROR_TYPE, ISelectQuery, IErrorType } from "@/common";
import { getDataType, LogHelper, removeSpace, promiseReject, getKeys } from "@/worker/utils";


Expand Down Expand Up @@ -128,7 +128,7 @@ class Join {
}
catch (ex) {
return promiseReject(
new LogHelper(ERROR_TYPE.InvalidOrderQuery, ex.message)
new LogHelper((ERROR_TYPE as IErrorType).InvalidOrderQuery, ex.message)
);
}
}
Expand Down
161 changes: 90 additions & 71 deletions src/worker/utils/log_helper.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ERROR_TYPE, IError } from "@/common";

export class LogHelper implements IError {
type: ERROR_TYPE;
type: string;
message: string;
private info_: any;

Expand All @@ -13,7 +13,7 @@ export class LogHelper implements IError {
}
}

constructor(type: ERROR_TYPE, info?) {
constructor(type: string, info?) {
this.type = type;
this.info_ = info;
this.message = this.getMsg_();
Expand All @@ -39,85 +39,104 @@ export class LogHelper implements IError {

private getMsg_() {
let errMsg: string;
switch (this.type) {
case ERROR_TYPE.NotArray:
const info = this.info_;
const errorHandler = {
[ERROR_TYPE.NotArray]() {
errMsg = "Supplied value is not an array";
break;
case ERROR_TYPE.UndefinedColumn:
},
[ERROR_TYPE.UndefinedColumn]() {
errMsg = "Column is undefined in Where";
break;
case ERROR_TYPE.UndefinedValue:
},
[ERROR_TYPE.UndefinedValue]() {
errMsg = "Value is undefined in Where";
break;
case ERROR_TYPE.UndefinedColumnName:
errMsg = "Column name is undefined '" + this.info_['TableName'] + "'";
break;
case ERROR_TYPE.UndefinedDbName:
},
[ERROR_TYPE.UndefinedColumnName]() {
errMsg = "Column name is undefined '" + info['TableName'] + "'";
},
[ERROR_TYPE.UndefinedDbName]() {
errMsg = "Database name is not supplied";
break;
case ERROR_TYPE.UndefinedColumnValue:
},
[ERROR_TYPE.UndefinedColumnValue]() {
errMsg = "Column value is undefined";
break;
case ERROR_TYPE.NoValueSupplied:
},
[ERROR_TYPE.NoValueSupplied]() {
errMsg = "No value is supplied";
break;
case ERROR_TYPE.InvalidOp:
errMsg = "Invalid Op Value '" + this.info_['Op'] + "'";
break;
case ERROR_TYPE.ColumnNotExist:
errMsg = this.info_['isOrder'] ?
`Column '${this.info_['column']}' in order query does not exist` :
`Column '${this.info_['column']}' does not exist`;
break;
case ERROR_TYPE.EnableSearchOff:
errMsg = "Search is turned off for the Column '" + this.info_['column'] + "'";
break;
case ERROR_TYPE.NullValue:
errMsg = "Null value is not allowed for column '" + this.info_['ColumnName'] + "'";
break;
case ERROR_TYPE.WrongDataType:
errMsg = "Supplied value for column '" + this.info_['column'] +
},
[ERROR_TYPE.InvalidOp]() {
errMsg = "Invalid Op Value '" + info['Op'] + "'";
},
[ERROR_TYPE.ColumnNotExist]() {
errMsg = info['isOrder'] ?
`Column '${info['column']}' in order query does not exist` :
`Column '${info['column']}' does not exist`;
},
[ERROR_TYPE.EnableSearchOff]() {
errMsg = "Search is turned off for the Column '" + info['column'] + "'";
},
[ERROR_TYPE.NullValue]() {
errMsg = "Null value is not allowed for column '" + info['ColumnName'] + "'";
},
[ERROR_TYPE.WrongDataType]() {
errMsg = "Supplied value for column '" + info['column'] +
"' have wrong data type";
break;
case ERROR_TYPE.TableNotExist:
errMsg = "Table '" + this.info_['tableName'] + "' does not exist";
break;
case ERROR_TYPE.DbNotExist:
errMsg = `Database with name ${this.info_['dbName']} does not exist`;
break;
case ERROR_TYPE.NotObject:
},
[ERROR_TYPE.TableNotExist]() {
errMsg = "Table '" + info['tableName'] + "' does not exist";
},
[ERROR_TYPE.DbNotExist]() {
errMsg = `Database with name ${info['dbName']} does not exist`;
},
[ERROR_TYPE.NotObject]() {
errMsg = "supplied value is not object";
break;
case ERROR_TYPE.InvalidOp:
errMsg = "Invalid Config '" + this.info_['Config'] + " '";
break;
case ERROR_TYPE.DbBlocked:
},
[ERROR_TYPE.InvalidOp]() {
errMsg = "Invalid Config '" + info['Config'] + " '";
},
[ERROR_TYPE.DbBlocked]() {
errMsg = `database is blocked, cant be deleted right now`;
break;
case ERROR_TYPE.NullValueInWhere:
errMsg = `Null/undefined is not allowed in where. Column '${this.info_['column']}' has null`;
break;
case ERROR_TYPE.MethodNotExist:
errMsg = `method '${this.info_}' does not exist.`;
break;
case ERROR_TYPE.IndexedDbNotSupported:
},
[ERROR_TYPE.NullValueInWhere]() {
errMsg = `Null/undefined is not allowed in where. Column '${info['column']}' has null`;
},
[ERROR_TYPE.MethodNotExist]() {
errMsg = `method '${info}' does not exist.`;
},
[ERROR_TYPE.IndexedDbNotSupported]() {
errMsg = "Browser does not support indexeddb";
break;
case ERROR_TYPE.InvalidJoinQuery:
case ERROR_TYPE.InvalidGroupQuery:
case ERROR_TYPE.InvalidOrderQuery:
case ERROR_TYPE.ImportScriptsFailed:
errMsg = this.info_;
break;
case ERROR_TYPE.InvalidMiddleware:
errMsg = `No function ${this.info_} is found.`;
break;
default:
if (!this.type) {
this.type = ERROR_TYPE.Unknown
}
errMsg = this.message;
break;
},
getInfo() {
errMsg = info;
},
[ERROR_TYPE.InvalidJoinQuery]() {
errorHandler.getInfo();
},
[ERROR_TYPE.ImportScriptsFailed]() {
errorHandler.getInfo();
},
[ERROR_TYPE.InvalidMiddleware]() {
errMsg = `No function ${info} is found.`;
},
};
if (process.env.NODE_ENV !== 'production') {
Object.assign(errorHandler, {
[ERROR_TYPE.InvalidOrderQuery]() {
errorHandler.getInfo();
},
[ERROR_TYPE.InvalidGroupQuery]() {
errorHandler.getInfo();
},
})
}
const errorType = this.type;
const method = errorHandler[errorType];
if (method) {
method();
}
else {
if (!errorType) {
this.type = ERROR_TYPE.Unknown
}
errMsg = this.message;
}
return errMsg;
}
Expand Down

0 comments on commit 671f66f

Please sign in to comment.