瀏覽代碼

Improve error handling

Add all validation errors.
master
TheoryOfNekomata 8 月之前
父節點
當前提交
785c4e03d8
共有 1 個文件被更改,包括 17 次插入2 次删除
  1. +17
    -2
      src/core.ts

+ 17
- 2
src/core.ts 查看文件

@@ -27,6 +27,7 @@ export interface Resource {
routeName?: string;
dataSource: DataSource;
newId(dataSource: DataSource): string | number | unknown;
schema: Parameters<typeof v.object>[0];
}

interface GenerationStrategy {
@@ -88,6 +89,9 @@ export const resource = (schema: Parameters<typeof v.object>[0]) => {
},
get routeName() {
return theRouteName;
},
get schema() {
return schema;
}
};
};
@@ -150,10 +154,21 @@ const handleCreate = async (
resolve();
return;
}
} catch {

await v.parseAsync(v.object(resource.schema), bodyDeserialized, { abortEarly: false });
} catch (err) {
res.statusCode = constants.HTTP_STATUS_BAD_REQUEST;
res.statusMessage = `Invalid ${resource.itemName}`;
res.end();

if (Array.isArray(err.issues)) {
// TODO better error reporting, localizable messages
res.end(
err.issues.map((i) => `${i.path.map((p) => p.key).join('.')}:\n${i.message}`)
.join('\n\n')
)
} else {
res.end();
}
resolve();
return;
}


Loading…
取消
儲存