errors.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 CustomError extends Error {
  27. constructor(message) {
  28. super(message);
  29. this.name = this.constructor.name;
  30. }
  31. }
  32. class TimeoutError extends CustomError {}
  33. exports.TimeoutError = TimeoutError;
  34. class TargetClosedError extends CustomError {
  35. constructor(cause, logs) {
  36. super((cause || 'Target page, context or browser has been closed') + (logs || ''));
  37. }
  38. }
  39. exports.TargetClosedError = TargetClosedError;
  40. function isTargetClosedError(error) {
  41. return error instanceof TargetClosedError || error.name === 'TargetClosedError';
  42. }
  43. function serializeError(e) {
  44. if ((0, _utils.isError)(e)) return {
  45. error: {
  46. message: e.message,
  47. stack: e.stack,
  48. name: e.name
  49. }
  50. };
  51. return {
  52. value: (0, _serializers.serializeValue)(e, value => ({
  53. fallThrough: value
  54. }))
  55. };
  56. }
  57. function parseError(error) {
  58. if (!error.error) {
  59. if (error.value === undefined) throw new Error('Serialized error must have either an error or a value');
  60. return (0, _serializers.parseSerializedValue)(error.value, undefined);
  61. }
  62. const e = new Error(error.error.message);
  63. e.stack = error.error.stack || '';
  64. e.name = error.error.name;
  65. return e;
  66. }