Няма описание
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

responseToError.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.responseToError = void 0;
  4. const _ = require("lodash");
  5. const error_1 = require("./error");
  6. function responseToError(response, body) {
  7. if (response.statusCode < 400) {
  8. return;
  9. }
  10. if (typeof body === "string") {
  11. if (response.statusCode === 404) {
  12. body = {
  13. error: {
  14. message: "Not Found",
  15. },
  16. };
  17. }
  18. else {
  19. body = {
  20. error: {
  21. message: body,
  22. },
  23. };
  24. }
  25. }
  26. if (typeof body !== "object") {
  27. try {
  28. body = JSON.parse(body);
  29. }
  30. catch (e) {
  31. body = {};
  32. }
  33. }
  34. if (!body.error) {
  35. const errMessage = response.statusCode === 404 ? "Not Found" : "Unknown Error";
  36. body.error = {
  37. message: errMessage,
  38. };
  39. }
  40. const message = "HTTP Error: " + response.statusCode + ", " + (body.error.message || body.error);
  41. let exitCode;
  42. if (response.statusCode >= 500) {
  43. exitCode = 2;
  44. }
  45. else {
  46. exitCode = 1;
  47. }
  48. _.unset(response, "request.headers");
  49. return new error_1.FirebaseError(message, {
  50. context: {
  51. body: body,
  52. response: response,
  53. },
  54. exit: exitCode,
  55. status: response.statusCode,
  56. });
  57. }
  58. exports.responseToError = responseToError;