utils.js 786 B

12345678910111213141516171819202122232425262728293031
  1. 'use strict';
  2. exports.detectErrorMessage = function(ctx, err) {
  3. // detect json parse error
  4. if (err.status === 400 &&
  5. err.name === 'SyntaxError' &&
  6. ctx.request.is('application/json', 'application/vnd.api+json', 'application/csp-report')) {
  7. return 'Problems parsing JSON';
  8. }
  9. return err.message;
  10. };
  11. exports.detectStatus = function(err) {
  12. // detect status
  13. let status = err.status || 500;
  14. if (status < 200) {
  15. // invalid status consider as 500, like urllib will return -1 status
  16. status = 500;
  17. }
  18. return status;
  19. };
  20. exports.accepts = function(ctx) {
  21. if (ctx.acceptJSON) return 'json';
  22. if (ctx.acceptJSONP) return 'js';
  23. return 'html';
  24. };
  25. exports.isProd = function(app) {
  26. return app.config.env !== 'local' && app.config.env !== 'unittest';
  27. };