Error Handling
The doc covers how to use handleAppError function to export logs to elastic search to catch error or creating logs in production.
Logs
- These are logs put in to monitor or collect metadata, and are not suppossed to be used for error handling.
- The metadata is any important data that needs to be exported to elasticSearch for future references. We can add any number of fields inside metadata.
- The err object has some conventions attached with it.
- "name" is kept as 'Logger' for OCS and 'Brand' for BRAND FILES.
- "message" is the details about the code section that needs to be logged. Like: 'Agent Kpis if condition', 'Agent Kpis else condition'
- "description" serves a bigger purpose. This is useful in the case when we want to group certain logs together under a heading.
- handleAppError object contains
- "err" object
- "metadata" object
- "scope" - Should be the function name, wherever possible.
Example Usecase
let metadata = { agentPsid: psid, adminIP: agentIP };
let err = { name: 'Logger', message: 'Agent Kpis', description: 'Agent Lifecycle Logs' };
handleAppError({ err, metadata, scope: 'agentKpi' });
Note
"name" should be equal to "Logger" or "Brand" always in case of these logs, passing in any other name would result it going to error logs!!
Error Logs
- There are two types of error-logs, one which are called from brand-files and another which are called from the rest of the OCS.
- We use status code to differentiate between errors from brand-files and OCS, this is important as brand-files is handled by separate teams.
- Status < 5000 indicates error originated from brand-files
- Status > 5000 indicates error originated from outside the brand-files
- Status = 5000 indicates error was passed in the wrong format and is not actually an error object
Example Usecase
function functionInBrandFiles = () => {
try {
throw new Error("This is an error");
} catch (err) {
// Pass in the err object as is, pass in
// the function name in scope and status code
handleAppError({ err, scope: "functionInBrandFiles", status: 1001 })
}
}
Note
Always pass function name in scope, this is a mandatory field for error logs
Note
Make sure to never throw anything without instantiating Error() class
throw "Wrong Format"; โ
throw new Error("Correct Format"); โ
Reserved Status Codes (Error Logs)
Important Note
Never pass a reserved status code from handleAppError
- 5000, For Logs passed in the wrong format that can be but not limited to:
- Name was passed something other than "Logger" or "Brand"
- Error was thrown which is not instantiated from the Error Class
- 3000, No status passed and error log in brand-files
- 3001, Wrong status passed and error log in brand-files
- 7000, No status passed and error log in ocs
- 7001, Wrong status passed and error log in ocs