first
This commit is contained in:
@@ -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
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
module.exports = {
|
||||
Success: {
|
||||
status: 200,
|
||||
obj: {
|
||||
resCode: 0,
|
||||
message: 'success'
|
||||
}
|
||||
},
|
||||
InternalError: {
|
||||
status: 500,
|
||||
obj: {
|
||||
resCode: 500,
|
||||
message: 'Internal Error'
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user