-first commit
This commit is contained in:
40
exthernal-accountingwep-api/src/utils/errorList.js
Normal file
40
exthernal-accountingwep-api/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
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";
|
||||
}
|
||||
}
|
||||
43
exthernal-accountingwep-api/src/utils/response.js
Normal file
43
exthernal-accountingwep-api/src/utils/response.js
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
|
||||
// ===================================================
|
||||
// 🧩 Unified Response Handler (vFinal+)
|
||||
// ===================================================
|
||||
// ===================================================
|
||||
// 📁 src/utils/response.js
|
||||
// ===================================================
|
||||
export function sendResponse(res, status, msg_th = null, msg_en = null, data = null) {
|
||||
const safeData = safeJson(data)
|
||||
const success = status < 400
|
||||
const response = {
|
||||
status: success ? 'succeed' : 'error',
|
||||
message: {
|
||||
th: msg_th ?? (success ? 'สำเร็จ' : 'เกิดข้อผิดพลาด'),
|
||||
en: msg_en ?? (success ? 'Succeed' : 'Error')
|
||||
},
|
||||
data: safeData
|
||||
}
|
||||
res.status(status).json(response)
|
||||
}
|
||||
|
||||
// ✅ ป้องกัน circular reference
|
||||
function safeJson(obj) {
|
||||
try {
|
||||
if (obj && typeof obj === 'object') {
|
||||
return JSON.parse(JSON.stringify(obj))
|
||||
}
|
||||
return obj
|
||||
} catch (err) {
|
||||
return '[Unserializable Object]'
|
||||
}
|
||||
}
|
||||
15
exthernal-accountingwep-api/src/utils/token.js
Normal file
15
exthernal-accountingwep-api/src/utils/token.js
Normal file
@@ -0,0 +1,15 @@
|
||||
import jwt from 'jsonwebtoken'
|
||||
import dotenv from 'dotenv'
|
||||
dotenv.config()
|
||||
|
||||
export function generateToken(payload) {
|
||||
return jwt.sign(payload, process.env.JWT_SECRET, { expiresIn: '24h' })
|
||||
}
|
||||
|
||||
export function verifyToken(token) {
|
||||
try {
|
||||
return jwt.verify(token, process.env.JWT_SECRET)
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
11
exthernal-accountingwep-api/src/utils/trim.js
Normal file
11
exthernal-accountingwep-api/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