Introduction
This document describes how to construct Error codes for all operations?
Background
Jira Issues
https://project-sunbird.atlassian.net/browse/SB-21578
Examples Of Error code
PFT_CCRT10 >> PBK_CCRT10 >> DMW_CCRT10 >> NBB_CCRT10 TraceId: 1234 Error happened at NodeBB end. NBB_CCRT10 << NBB_CCRT10 << NBB_CCRT10 << NBB_CCRT10 ===> Servies: Discussion Middleware service -> DMW NodeBB Sercie -> NBB ===> Operations: Category create - CCRT Topic create - TCRT Group create - GCRT Usr create - UCRT ===> ERROR CODES: 01 - Category creation failed because of reason 1? 02 - Category create failed because of reason 2? 03 - Category update failed because of reason 1? 04 - Category update failed because of reason 2? ... DMW_CCRT01 NBB_GCRT{error_code}
Problem Statement:
How to assign specific error code to api?
Solution:
API | Unique Operation Name | Operation Name | Error Number | Error Description | Error Code |
FORUM API's | |||||
/forum/v2/read | FRED | DMW_FRED | 01 | Forum read failed because plugin not enabled. | DMW_FRED01 |
/forum/v2/create | FCRT | DMW_FCRT | 02 | Forum create failed because plugin not enabled. | DMW_FCRT02 |
Tags API | |||||
/tags | TRED | DMW_TRED | 03 | Tags read failed | DMW_TRED03 |
USER DETAILS API's | |||||
/user/:userslug | URED | DMW_URED | 04 | User details read failed | DMW_URED04 |
/user/:userslug/upvoted | UUVR | DMW_URED | 05 | User up vote post details read failed | DMW_URED05 |
/user/:userslug/downvoted | UDVR | DMW_URED | 06 | User down vote post details read failed | DMW_URED06 |
/user/:userslug/bookmarks | UBMR | DMW_URED | 07 | User saved post details read failed | DMW_URED07 |
/user/v1/create | UCRT | DMW_UCRT | 08 | Create user failed | DMW_UCRT08 |
CATEGORY API's | |||||
/category/:category_id/:slug | CDTL | DMW_CRED | 09 | Category details read failed. | DMW_CRED09 |
/category/:cid | CRED | DMW_CRED | |||
/categories | CLST | DMW_CRED | 10 | Access list of categories failed. | DMW_CRED10 |
/v2/categories | CCRT | DMW_CCRT | 11 | Create category failed | DMW_CCRT11 |
/v2/categories/:cid(PUT) | CUDT | DMW_CUDT | 12 | Update category failed | DMW_CUDT12 |
/v2/categories/:cid(DELETE) | CDEL | DMW_CDEL | 13 | Delete category failed | DMW_CDEL13 |
TOPICS API's | |||||
/unread | TUNR | DMW_TRED | 14 | Access unread topics failed | DMW_TRED14 |
/recent | TRCT | DMW_TRED | 15 | Access recent topics failed. | DMW_TRED15 |
/topic/:topic_id/:slug | TDTL | DMW_TRED | 16 | Access topic details failed | DMW_TRED16 |
/v2/topics | TCRT | DMW_TCRT | 17 | Create topic failed | DMW_TCRT17 |
/v2/topics/:tid(POST) | RCRT | DMW_RCRT | 18 | Add reply failed | DMW_RCRT18 |
/v2/topics/:tid(PUT) | TUDT | DMW_TUDT | 19 | Update topics/reply failed | DMW_TUDT19 |
/v2/topics/:tid(DELETE) | TDEL | DMW_TDEL | 20 | Delete topic/reply failed | DMW_TDEL20 |
POST API's | |||||
/post/pid/:pid | PRED | DWM_PRED | 21 | Access post details failed | DWM_PRED21 |
/v2/posts/:pid(POST) | PCRT | DMW_PCRT | 22 | Create post failed | DMW_PCRT22 |
/v2/posts/:pid(DELETE) | PDEL | DWM_PDEL | 23 | Delete post failed | DWM_PDEL23 |
/v2/posts/:pid/vote(POST) | PVOT | DMW_PCRT | 24 | Adding vote to a post failed | DMW_PCRT24 |
/v2/posts/:pid/vote(DELETE) | PVDL | DMW_PCRT | 25 | Deleting vote failed | DMW_PCRT25 |
/v2/posts/:pid/bookmark(POST) | PSVE | DMW_PCRT | 26 | Bookmark post failed | DMW_PCRT26 |
/v2/posts/:pid/bookmark(DELETE) | PUSVE | DMW_PCRT | 27 | Delete bookmark failed | DMW_PCRT27 |
Challenges:
We can not tract nodebb api errors, because we are only accessing nodebb api, we not doing code changes.
We can track only discussion forum Errors and Portal Errors. But In Middleware and Portal backend we are doing proxy calls only, so we can tract only code issues instead of actual api issues.
How to get proper error code for a particular api in discussion middleware?
To get the proper error code for a particular api, we are maintain a json object like below
{ '/discussions/forum/v2/read': { 'errorMsg': "Forum read api failed because of plugin not enabled", 'err': "DMW_FRED01" }, '/discussions/forum/v2/creat': { 'errorMsg': "Forum creat api failed because of plugin not enabled", 'err': "DMW_FCRT02" }, '/discussions/v2/topic': { 'errorMsg': "Create topic failed", 'err': "DMW_TCRT17" }, ..... ..... ..... }
in Proxy object we are mapping the error code like below
**** Import Error Json file here as errorCodes ***** app.post(`${BASE_REPORT_URL}/forum/v2/read`, proxyObject()); app.post(`${BASE_REPORT_URL}/forum/v2/create`, proxyObject()); app.post(`${BASE_REPORT_URL}/v2/topics`, proxyObject()); function proxyObject() { return proxy(nodebbServiceUrl, { proxyReqOptDecorator: proxyUtils.decorateRequestHeaders(), proxyReqPathResolver: function (req) { let urlParam = req.originalUrl.replace('/discussion', ''); let query = require('url').parse(req.url).query; if (query) { return require('url').parse(nodebbServiceUrl+ urlParam + '?' + query).path } else { const incomingUrl = req.protocol + '://' + req.get('host') + req.originalUrl; const proxyUrl = require('url').parse(nodebbServiceUrl + urlParam); return proxyUrl.path; } }, userResDecorator: (proxyRes, proxyResData, req, res) => { try { const data = (proxyResData.toString('utf8')); if (proxyRes.statusCode === 404 ) { return data; } else { return proxyUtils.handleSessionExpiry(proxyRes, proxyResData, req, res, data); } } catch (err) { *** Here is mapping happned const errorObj = errorCodes[req.originalUrl]; { "id": "discussion.forum.middleware", "ver": "1.0", "ts": "2020-11-25T07:18:09.945Z", "params": { "resmsgid": "5f36c090-2eee-11eb-80ed-6bb70096c082", "msgid": "req.headers['x-requester-id']", "status": "failed", "err": errorObj.err, "errmsg": errorObj .errorMsg } } *** return proxyUtils.handleSessionExpiry(proxyRes, proxyResData, req, res); } } }) }