timer-logs
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -16,2 +16,12 @@ "use strict"; | ||
}).catch(timer.genericErrorCustomMessage('A better explanation for what caused this error')); | ||
timer.flush(); | ||
const postgresExample = async () => { | ||
const { rows } = await new Promise((resolve => setTimeout(resolve, 50))) | ||
.then(() => { | ||
throw new Error('Unexpected error occured'); | ||
return { rows: ['row1', 'row2'] }; | ||
}) | ||
.catch(timer.postgresErrorReturn({ rows: [] })); | ||
}; | ||
postgresExample().then(() => { | ||
timer.flush(); | ||
}); |
@@ -22,3 +22,13 @@ import Timer from '../index' | ||
// always call flush at the end of the file (before the return statement) to print out the log | ||
timer.flush() | ||
const postgresExample = async () => { | ||
const { rows } = await new Promise((resolve => setTimeout(resolve, 50))) | ||
.then(()=> { | ||
throw new Error('Unexpected error occured') | ||
return {rows: ['row1', 'row2']} | ||
}) | ||
.catch(timer.postgresErrorReturn({rows:[]})) | ||
} | ||
postgresExample().then(()=>{ | ||
// always call flush at the end of the file (before the return statement) to print out the log | ||
timer.flush() | ||
}) |
@@ -125,9 +125,21 @@ declare type Config = { | ||
* @param e the error object returned by postgres client | ||
* | ||
* @param returnVal specify a custom value to be returned. Defaults to null. | ||
* @return null so the promise resolves to a value | ||
* @example | ||
* const { rows } = await pool.query('SELECT NOW()',[]) | ||
* const result = await pool.query('SELECT NOW()',[]) | ||
* .catch(e=>timer.postgresError(e)) | ||
*/ | ||
postgresError(e: PostgresError): void; | ||
postgresError(e: PostgresError, returnVal?: any): any; | ||
/** | ||
* Convenience wrapper for postgresError, to return a value. | ||
* By default it returns null, but can be overriden with this method. | ||
* This is useful if you want your promise to resolve to a default value | ||
* in case of an error. | ||
* @param returnValue the value to return | ||
* @example | ||
* const { rows } = await pool.query('SELECT NOW()',[]) | ||
* .catch(e=>timer.postgresErrorReturn({rows:[]})) | ||
*/ | ||
postgresErrorReturn(returnValue: any): (e: PostgresError) => any; | ||
/** | ||
* Logs a generic error in a separate log to the main Timer. | ||
@@ -158,3 +170,6 @@ * | ||
* @example | ||
* await new Promise | ||
* await new Promise((resolve => setTimeout(resolve, 50))) | ||
* .then(()=> { | ||
* throw new Error('Unexpected error occured') | ||
* }).catch(timer.genericErrorCustomMessage('A better explanation for what caused this error')) | ||
*/ | ||
@@ -161,0 +176,0 @@ genericErrorCustomMessage(message: string): (e: Error) => void; |
@@ -101,7 +101,11 @@ "use strict"; | ||
} | ||
postgresError(e) { | ||
postgresError(e, returnVal) { | ||
const errorDetails = new Map(Object.entries(e)); | ||
errorDetails.set("databaseType", "postgres"); | ||
this.printLog(errorDetails, Severity.ERROR); | ||
return returnVal !== null && returnVal !== void 0 ? returnVal : null; | ||
} | ||
postgresErrorReturn(returnValue) { | ||
return (e) => this.postgresError(e, returnValue); | ||
} | ||
genericError(e, message) { | ||
@@ -108,0 +112,0 @@ const errorDetails = new Map([ |
27
index.ts
@@ -205,14 +205,30 @@ import * as crypto from 'crypto' | ||
* @param e the error object returned by postgres client | ||
* | ||
* @param returnVal specify a custom value to be returned. Defaults to null. | ||
* @return null so the promise resolves to a value | ||
* @example | ||
* const { rows } = await pool.query('SELECT NOW()',[]) | ||
* const result = await pool.query('SELECT NOW()',[]) | ||
* .catch(e=>timer.postgresError(e)) | ||
*/ | ||
public postgresError(e: PostgresError): void { | ||
public postgresError(e: PostgresError, returnVal?: any): any { | ||
const errorDetails = new Map(Object.entries(e)) | ||
errorDetails.set("databaseType", "postgres") | ||
this.printLog(errorDetails, Severity.ERROR) | ||
return returnVal ?? null | ||
} | ||
/** | ||
* Convenience wrapper for postgresError, to return a value. | ||
* By default it returns null, but can be overriden with this method. | ||
* This is useful if you want your promise to resolve to a default value | ||
* in case of an error. | ||
* @param returnValue the value to return | ||
* @example | ||
* const { rows } = await pool.query('SELECT NOW()',[]) | ||
* .catch(e=>timer.postgresErrorReturn({rows:[]})) | ||
*/ | ||
public postgresErrorReturn(returnValue: any) { | ||
return (e: PostgresError) => this.postgresError(e, returnValue) | ||
} | ||
/** | ||
* Logs a generic error in a separate log to the main Timer. | ||
@@ -254,3 +270,6 @@ * | ||
* @example | ||
* await new Promise | ||
* await new Promise((resolve => setTimeout(resolve, 50))) | ||
* .then(()=> { | ||
* throw new Error('Unexpected error occured') | ||
* }).catch(timer.genericErrorCustomMessage('A better explanation for what caused this error')) | ||
*/ | ||
@@ -257,0 +276,0 @@ public genericErrorCustomMessage(message: string) { |
{ | ||
"name": "timer-logs", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"devDependencies": { | ||
@@ -5,0 +5,0 @@ "@types/node": "^15.00.0" |
Sorry, the diff of this file is not supported yet
53670
1044