Skip to content

Commit

Permalink
Merge pull request #3023 from LiteFarmOrg/LF-3881-return-better-error…
Browse files Browse the repository at this point in the history
…s-by-default-from-the-api

LF-3881 Return better errors by default from the API
  • Loading branch information
SayakaOno authored Dec 11, 2023
2 parents 3ae4532 + 7c6c047 commit 272a68f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 3 deletions.
16 changes: 13 additions & 3 deletions packages/api/src/common/logger.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
import winston from 'winston';
import winston, { format } from 'winston';
import DailyRotateFile from 'winston-daily-rotate-file';
import Transport from 'winston-transport';
import * as Sentry from '@sentry/node';

const { errors, json, combine } = format;

// Add the error message as an enumerable property to return with res.json({ error })
const enumerateErrorMessage = format((info) => {
if (info instanceof Error) {
info.error = { message: info.message };
}
return info;
});

const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
format: combine(enumerateErrorMessage(), json()),
defaultMeta: { service: 'user-service' },
transports: [
//
Expand All @@ -25,7 +35,7 @@ const logger = winston.createLogger({
if (process.env.NODE_ENV !== 'production') {
logger.add(
new winston.transports.Console({
format: winston.format.simple(),
format: combine(errors(), json()),
}),
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/controllers/cropVarietyController.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const cropVarietyController = {
const result = await CropVarietyModel.query().whereNotDeleted().where({ farm_id });
return res.status(200).send(result);
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
};
Expand All @@ -33,6 +34,7 @@ const cropVarietyController = {
? res.status(200).send(result)
: res.status(404).send('Crop variety not found');
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/controllers/documentController.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const documentController = {
.patch({ archived: req.body.archived });
return result ? res.sendStatus(200) : res.status(404).send('Document not found');
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/controllers/insightController.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ const insightController = {
res.status(200).send({});
}
} catch (error) {
console.error(error);
res.status(400).json({ error });
}
};
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/controllers/managementPlanController.js
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,7 @@ const managementPlanController = {
? res.status(200).send(removeCropVarietyFromManagementPlans(managementPlans))
: res.status(404).send('Field crop not found');
} catch (error) {
console.error(error);
res.status(400).json({ error });
}
};
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/controllers/organicHistoryController.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default {
const result = await OrganicHistoryModel.query().context(req.auth).insert(req.body);
return res.status(201).send(result);
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
};
Expand All @@ -36,6 +37,7 @@ export default {
? res.status(200).send(result)
: res.status(404).send('Organic history not found');
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
},
Expand Down
2 changes: 2 additions & 0 deletions packages/api/src/controllers/taskController.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ const taskController = {
}
return res.status(404).send('Tasks not found');
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
},
Expand Down Expand Up @@ -813,6 +814,7 @@ const taskController = {

return res.status(200).send(result);
} catch (error) {
console.error(error);
return res.status(400).json({ error });
}
},
Expand Down

0 comments on commit 272a68f

Please sign in to comment.