-first commit
This commit is contained in:
41
exthernal-accountingwep-api/src/utils/oftenError.js
Normal file
41
exthernal-accountingwep-api/src/utils/oftenError.js
Normal file
@@ -0,0 +1,41 @@
|
||||
// utils/oftenError.js
|
||||
import { manualError } from "./errorList.js";
|
||||
|
||||
export class OftenError extends Error {
|
||||
/**
|
||||
* ใช้ได้ 2 แบบ:
|
||||
* 1. throw new OftenError("not_found")
|
||||
* 2. throw new OftenError(400, "ไทย", "English")
|
||||
*/
|
||||
constructor(arg1, arg2, arg3) {
|
||||
// แบบ lookup จาก key
|
||||
if (typeof arg1 === "string" && !arg2 && !arg3) {
|
||||
const found = manualError(arg1);
|
||||
super(found.messageEn);
|
||||
this.statusCode = found.code;
|
||||
this.messageTh = found.messageTh;
|
||||
this.messageEn = found.messageEn;
|
||||
this.key = arg1;
|
||||
}
|
||||
|
||||
// แบบ manual
|
||||
else if (typeof arg1 === "number" && arg2 && arg3) {
|
||||
super(arg3);
|
||||
this.statusCode = arg1;
|
||||
this.messageTh = arg2;
|
||||
this.messageEn = arg3;
|
||||
this.key = "manual";
|
||||
}
|
||||
|
||||
// fallback
|
||||
else {
|
||||
super("Invalid error format");
|
||||
this.statusCode = 500;
|
||||
this.messageTh = "รูปแบบการสร้าง error ไม่ถูกต้อง";
|
||||
this.messageEn = "Invalid error constructor format";
|
||||
this.key = "invalid_format";
|
||||
}
|
||||
|
||||
this.name = "OftenError";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user