Browse Source

Improve error handling

Add all validation errors.
master
TheoryOfNekomata 6 months ago
parent
commit
785c4e03d8
1 changed files with 17 additions and 2 deletions
  1. +17
    -2
      src/core.ts

+ 17
- 2
src/core.ts View File

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


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

await v.parseAsync(v.object(resource.schema), bodyDeserialized, { abortEarly: false });
} catch (err) {
res.statusCode = constants.HTTP_STATUS_BAD_REQUEST; res.statusCode = constants.HTTP_STATUS_BAD_REQUEST;
res.statusMessage = `Invalid ${resource.itemName}`; 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(); resolve();
return; return;
} }


Loading…
Cancel
Save