feat: import #831
@ -9,7 +9,8 @@ export const importController: RequestHandler = async (req, res) => {
|
|||||||
const inventory = await getInventory(accountId);
|
const inventory = await getInventory(accountId);
|
||||||
const request = JSON.parse(String(req.body)) as IImportRequest;
|
const request = JSON.parse(String(req.body)) as IImportRequest;
|
||||||
|
|||||||
importInventory(inventory, request.inventory);
|
importInventory(inventory, request.inventory);
|
||||||
res.json(await inventory.save());
|
await inventory.save();
|
||||||
|
res.end();
|
||||||
};
|
};
|
||||||
![]() 🛠️ Refactor suggestion Implement error handling during import Operations like Wrap the import operations in a try-catch block and handle errors appropriately:
_:hammer_and_wrench: Refactor suggestion_
**Implement error handling during import**
Operations like `importInventory` and `inventory.save()` can fail. Without error handling, any failures won't be communicated back to the client, and the server might crash due to unhandled exceptions.
Wrap the import operations in a try-catch block and handle errors appropriately:
```diff
+ try {
const inventory = await getInventory(accountId);
importInventory(inventory, request.inventory);
await inventory.save();
+ } catch (error) {
+ res.status(500).json({ error: 'Failed to import inventory' });
+ return;
+ }
```
> Committable suggestion skipped: line range outside the PR's diff.
<!-- This is an auto-generated comment by CodeRabbit -->
|
|||||||
|
|
||||||
interface IImportRequest {
|
interface IImportRequest {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user
⚠️ Potential issue
Avoid unnecessary parsing of
req.body
Express, when configured with the appropriate middleware, automatically parses JSON request bodies. Directly calling
JSON.parse(String(req.body))
may not be necessary and could lead to errors ifreq.body
is already an object.Modify the code to use the parsed body directly:
Ensure that
express.json()
middleware is applied to parse JSON request bodies.📝 Committable suggestion