errors.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.TimeoutError = exports.TargetClosedError = void 0;
  6. exports.isTargetClosedError = isTargetClosedError;
  7. exports.parseError = parseError;
  8. exports.serializeError = serializeError;
  9. var _utils = require("../utils");
  10. var _serializers = require("../protocol/serializers");
  11. /**
  12. * Copyright (c) Microsoft Corporation.
  13. *
  14. * Licensed under the Apache License, Version 2.0 (the "License");
  15. * you may not use this file except in compliance with the License.
  16. * You may obtain a copy of the License at
  17. *
  18. * http://www.apache.org/licenses/LICENSE-2.0
  19. *
  20. * Unless required by applicable law or agreed to in writing, software
  21. * distributed under the License is distributed on an "AS IS" BASIS,
  22. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23. * See the License for the specific language governing permissions and
  24. * limitations under the License.
  25. */
  26. class TimeoutError extends Error {
  27. constructor(message) {
  28. super(message);
  29. this.name = 'TimeoutError';
  30. }
  31. }
  32. exports.TimeoutError = TimeoutError;
  33. class TargetClosedError extends Error {
  34. constructor(cause) {
  35. super(cause || 'Target page, context or browser has been closed');
  36. }
  37. }
  38. exports.TargetClosedError = TargetClosedError;
  39. function isTargetClosedError(error) {
  40. return error instanceof TargetClosedError;
  41. }
  42. function serializeError(e) {
  43. if ((0, _utils.isError)(e)) return {
  44. error: {
  45. message: e.message,
  46. stack: e.stack,
  47. name: e.name
  48. }
  49. };
  50. return {
  51. value: (0, _serializers.serializeValue)(e, value => ({
  52. fallThrough: value
  53. }))
  54. };
  55. }
  56. function parseError(error) {
  57. if (!error.error) {
  58. if (error.value === undefined) throw new Error('Serialized error must have either an error or a value');
  59. return (0, _serializers.parseSerializedValue)(error.value, undefined);
  60. }
  61. if (error.error.name === 'TimeoutError') {
  62. const e = new TimeoutError(error.error.message);
  63. e.stack = error.error.stack || '';
  64. return e;
  65. }
  66. if (error.error.name === 'TargetClosedError') {
  67. const e = new TargetClosedError(error.error.message);
  68. e.stack = error.error.stack || '';
  69. return e;
  70. }
  71. const e = new Error(error.error.message);
  72. e.stack = error.error.stack || '';
  73. e.name = error.error.name;
  74. return e;
  75. }