Becoditive-API
Becoditive-API copied to clipboard
RFC: Maintain an object tree to represent HTTP, database and response status and message instead of using magic numbers
Is your feature request related to a problem? Please describe. Using magic numbers like 500, 401 in error handling, etc is not descriptive.
Describe the solution you'd like Create a simple object tree for controllers use, to represent HTTP, database and both response status code and message.
Just an example below:
// Src/Controllers/[rR]esponse.js
module.exports = {
http: {
'BAD_REQUEST': {
'code': 400,
'message': 'Invalid API request'
},
'UNAUTHORIZED': {
'code': 401,
'message': 'Unauthorized access'
},
'NOT_FOUND': {
'code': 404,
'message': 'Not found'
},
'OK': {
'code': 200,
'message': 'Request succeed'
}
},
database: {
'DUPLICATE': {
'code': 11000,
'message': 'Database record duplication'
}
}
}
response.http.BAD_REQUEST.code // 400
response.http.BAD_REQUEST.message // Invalid API request
response.http.OK.code // 200
response.http.OK.message // Request succeed
response.database.DUPLICATE.message // Database record duplication
In the future, this object could be reused in more exception/logging heavy error handling parts if it comes to that. Adding, removing stuff from this response object should be quite easy too, since it doesn't depends on anything else.
What do you think?
Thanks for reviewing.
Hmm Yeah Everything will be changed thank for suggesting
But i have to also send data alongside the http status so how will be that possible ?
Just send the data like usual? The goal of this tree is to replace cryptic code like 500, 401 with something more readable and accessible from a single place.
Example:
res.status(response.http.OK.code).json({
logo: randomData.logo,
answer: randomData.answer
})
If you have custom response message, you could just send the plain response message directly. Basically, you don't have to use response.http.RESPONSE_TYPE.message. Simply, use 'my response message'.
Does that answer your question? :3
hmmmm OK thanks