-first commit
This commit is contained in:
40
@template/src/utils/errorList.js
Normal file
40
@template/src/utils/errorList.js
Normal file
@@ -0,0 +1,40 @@
|
||||
// utils/errorList.js
|
||||
|
||||
export function manualError(key) {
|
||||
switch (key) {
|
||||
case "invalid_input":
|
||||
return {
|
||||
code: 400,
|
||||
messageTh: "ข้อมูลที่ส่งมาไม่ถูกต้อง",
|
||||
messageEn: "Invalid input data"
|
||||
};
|
||||
|
||||
case "not_found":
|
||||
return {
|
||||
code: 404,
|
||||
messageTh: "ไม่พบข้อมูลที่ร้องขอ",
|
||||
messageEn: "Resource not found"
|
||||
};
|
||||
|
||||
case "unauthorized":
|
||||
return {
|
||||
code: 401,
|
||||
messageTh: "คุณไม่มีสิทธิ์เข้าถึงข้อมูลนี้",
|
||||
messageEn: "Unauthorized access"
|
||||
};
|
||||
|
||||
case "server_error":
|
||||
return {
|
||||
code: 500,
|
||||
messageTh: "เกิดข้อผิดพลาดภายในระบบ",
|
||||
messageEn: "Internal server error"
|
||||
};
|
||||
|
||||
default:
|
||||
return {
|
||||
code: 500,
|
||||
messageTh: "ข้อผิดพลาดที่ไม่ทราบสาเหตุ",
|
||||
messageEn: "Unknown error occurred"
|
||||
};
|
||||
}
|
||||
}
|
||||
41
@template/src/utils/oftenError.js
Normal file
41
@template/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";
|
||||
}
|
||||
}
|
||||
29
@template/src/utils/response.js
Normal file
29
@template/src/utils/response.js
Normal file
@@ -0,0 +1,29 @@
|
||||
/**
|
||||
* sendResponse
|
||||
* ----------------------------------------------
|
||||
* ส่ง response แบบมาตรฐาน รองรับข้อความ 2 ภาษา
|
||||
* ----------------------------------------------
|
||||
* @param {object} res - Express response object
|
||||
* @param {number} status - HTTP Status code (200, 400, 500, etc.)
|
||||
* @param {string} msg_th - ข้อความภาษาไทย
|
||||
* @param {string} msg_en - ข้อความภาษาอังกฤษ
|
||||
* @param {any} [data=null] - optional data
|
||||
*/
|
||||
export function sendResponse(res, status = 200, msg_th = null, msg_en = null, data = null) {
|
||||
const isError = status >= 400
|
||||
|
||||
// ✅ ถ้าไม่ใช่ error และไม่มีข้อความ → ใช้ข้อความ default
|
||||
const message_th = msg_th || (isError ? 'เกิดข้อผิดพลาด' : 'สำเร็จ')
|
||||
const message_en = msg_en || (isError ? 'Error occurred' : 'Succeed')
|
||||
|
||||
const response = {
|
||||
status: isError ? 'error' : 'succeed',
|
||||
message: {
|
||||
th: message_th,
|
||||
en: message_en
|
||||
},
|
||||
data
|
||||
}
|
||||
|
||||
res.status(status).json(response)
|
||||
}
|
||||
11
@template/src/utils/trim.js
Normal file
11
@template/src/utils/trim.js
Normal file
@@ -0,0 +1,11 @@
|
||||
export function trim_all_array(data) {
|
||||
if (!Array.isArray(data)) return data
|
||||
for (let row of data) {
|
||||
for (let key in row) {
|
||||
if (typeof row[key] === 'string') {
|
||||
row[key] = row[key].trim()
|
||||
}
|
||||
}
|
||||
}
|
||||
return data
|
||||
}
|
||||
Reference in New Issue
Block a user