12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- var error = require("./Errors");
- module.exports = class Response {
- constructor(code, msg, data)
- {
- this.code=0;
- this.message="Success";
- this.data=null;
- if(code!=undefined) this.code=code;
- if(code!=undefined) this.message=msg;
- if(data!=undefined) this.data=data;
- }
- getCode() {return this.code;}
- getMessage() {return this.message;}
- getData() {return this.data;}
- setCode(c){ this.code = c; }
- setMessage(c){ this.message = c; }
- setData(c){ this.data = c; }
- toString()
- {
- return JSON.stringify(this);
- }
- static fromError(code, data)
- {
- var msg = error.messages[code][0];
- var r = new Response(code, msg, data)
- if(data==undefined) delete r.data;
- return r;
- }
- static writeResponse(res, err, data)
- {
- res.setHeader("Content-Type", "application/json");
- console.log("1 : "+err);
- res.status(error.messages[err][1]);
- res.end(JSON.stringify(Response.fromError(err, data)));
- if(error.messages[err][1]>=400) return null;
- return true;
- }
- //std error
- static success(data) { return Response.fromError(error.ESUCCESS, data); }
- static noent(data) { return Response.fromError(error.ENOENT, data); }
- static perm(data) { return Response.fromError(error.EPERM, data); }
- static badArgs(data) { return Response.fromError(error.EINVAL, data);}
- static badUser(data) { return Response.fromError(error.BAD_USER, data); }
- static userExists(data) { return Response.fromError(error.USER_EXISTS, data); }
- static badProject(data) { return Response.fromError(error.BAD_PROJECT, data); }
- static projectExists(data) { return Response.fromError(error.PROJECT_EXISTS, data); }
- static badRes(data) { return Response.fromError(error.BAD_RES, data); }
- static resExists(data) { return Response.fromError(error.RES_EXISTS, data); }
- }
|