This commit is contained in:
Jay
2018-03-13 23:34:27 +08:00
commit d4f9e78e2a
15 changed files with 396 additions and 0 deletions
+65
View File
@@ -0,0 +1,65 @@
const message = require('./message')
/**
* api response message util
* @param {String} key
* @param {String} msg
*/
const resObject = function (key = '', msg = null) {
if (!key || !(key in message)) {
// default message
return
}
let obj = Object.assign({}, message[key])
if (typeof msg === 'string' && msg.length > 0) obj.obj.message = msg
if (msg !== null && typeof msg === 'object') obj.obj = msg
return obj
}
const genError = function (type, msg = '') {
let err = new MError()
err.mtype = type
err.msg = msg
return err
}
class MError extends Error {
/**
* @param {String} str
*/
constructor (str) {
super(str)
this._mtype = ''
this._msg = ''
}
/**
* @param {String} type
*/
set mtype (type = '') {
this._mtype = type
}
get mtype () {
return this._mtype
}
/**
* @param {String} s
*/
set msg (s = '') {
this._msg = s
}
get msg () {
return this._msg
}
}
module.exports = {
resObject,
MError,
genError
}
+16
View File
@@ -0,0 +1,16 @@
module.exports = {
Success: {
status: 200,
obj: {
resCode: 0,
message: 'success'
}
},
InternalError: {
status: 500,
obj: {
resCode: 500,
message: 'Internal Error'
}
}
}