feat: enhance /payer route with email format validation and update HttpError class to use ErrorDescEnum

This commit is contained in:
Astrian Zheng 2025-01-11 22:06:57 +11:00
parent 6c6936097e
commit be9a4c21ee
Signed by: Astrian
SSH Key Fingerprint: SHA256:rVnhx3DAKjujCwWE13aDl7uV6+9U1MvydLkNRXJrBiA
2 changed files with 24 additions and 4 deletions

View File

@ -19,9 +19,11 @@ app.use(route.post('/payer', async (ctx) => {
// TODO: 请求头验证 bearer token // TODO: 请求头验证 bearer token
const { name, address, email, abn } = ctx.request.body const { name, address, email, abn } = ctx.request.body
if (!name || !address || !email) { if (!name || !address || !email) ctx.throw(400, 'required_fields_missing')
ctx.throw(400, 'required_fields_missing')
} // 验证邮箱格式
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
if (!emailRegex.test(email)) ctx.throw(400, 'invalid_field_format')
try { try {
const payer = await func.createPayer(name, address, email, abn) const payer = await func.createPayer(name, address, email, abn)

View File

@ -8,6 +8,24 @@ class HttpError extends Error {
* @param message * @param message
* @param status * @param status
*/ */
constructor(message: 'required_fields_missing' | 'unknown_issues', status: number); constructor(message: ErrorDescEnum, status: number);
status: 400 | 401 | 403 | 404 | 500; status: 400 | 401 | 403 | 404 | 500;
} }
/**
*
*/
enum ErrorDescEnum {
/**
* HTTP 400 Bad Request 使
*/
required_fields_missing = 'required_fields_missing',
/**
* HTTP 400 Bad Request 使
*/
invalid_field_format = 'invalid_field_format',
/**
* HTTP 500 Internal Server Error 使
*/
unknown_issues = 'unknown_issues'
}