Source: client/errors.js

  1. /*
  2. * Copyright 2023, TeamDev. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Redistribution and use in source and/or binary forms, with or without
  11. * modification, must retain the above copyright notice and the following
  12. * disclaimer.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  16. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  17. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  18. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  19. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  20. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. "use strict";
  27. import {Error as SpineBaseError} from '../proto/spine/base/error_pb';
  28. import {ValidationError} from '../proto/spine/validate/validation_error_pb';
  29. import {isProtobufMessage} from './typed-message';
  30. /**
  31. * The base error type of Spine Web. This error type is only used directly when a
  32. * more appropriate category is not defined for the offending error.
  33. *
  34. * @extends Error
  35. */
  36. export class SpineError extends Error {
  37. /**
  38. * @param {!string} message the human-readable error message
  39. * @param {*=} cause the reason why this error occurred
  40. */
  41. constructor(message, cause) {
  42. super(message);
  43. this.name = this.constructor.name;
  44. this._cause = cause;
  45. }
  46. /**
  47. * @return {*|undefined} The cause of this error, if available.
  48. */
  49. getCause() {
  50. return this._cause;
  51. }
  52. }
  53. /**
  54. * An error which occurs when sending off a request to Spine server endpoint fails due to the
  55. * connection problems.
  56. *
  57. * Can be caused by an incorrect server address, lack of network connectivity or
  58. * if the response is not received from the server.
  59. *
  60. * @extends SpineError
  61. */
  62. export class ConnectionError extends SpineError {
  63. /**
  64. * @param {!Error} error the error caught from {@code fetch} invocation
  65. */
  66. constructor(error) {
  67. super(error.message, error);
  68. }
  69. }
  70. /**
  71. * An error which occurs when sending off a request to Spine server endpoint results
  72. * with a response with `5xx` status code.
  73. *
  74. * @extends SpineError
  75. */
  76. export class ServerError extends SpineError {
  77. /**
  78. * @param {!Response} response the server response caused this error
  79. */
  80. constructor(response) {
  81. super(response.statusText, response);
  82. }
  83. }
  84. /**
  85. * An error which occurs when sending off a request to Spine server endpoint results
  86. * with a response with `4xx` status code.
  87. *
  88. * @extends SpineError
  89. */
  90. export class ClientError extends SpineError {
  91. /**
  92. * @param {!string} message the human-readable error message
  93. * @param {*=} cause the reason why this error occurred
  94. */
  95. constructor(message, cause) {
  96. super(message, cause);
  97. }
  98. }
  99. /**
  100. * An error which occurs when sending off a command to Spine server endpoint.
  101. *
  102. * @extends SpineError
  103. */
  104. export class CommandHandlingError extends SpineError {
  105. /**
  106. * @param {!string} message the human-readable error message
  107. * @param {!Error|SpineError|SpineBaseError} error the reason why this error occurred
  108. */
  109. constructor(message, error) {
  110. super(message, error);
  111. }
  112. /**
  113. * Returns `true` if the command wasn't accepted by the server; returns `false`
  114. * if this is not guaranteed.
  115. *
  116. * A command is assumed neglected if this error is caused by the `ClientError`
  117. * or the `SpineBaseError`.
  118. *
  119. * @return {boolean}
  120. */
  121. assuresCommandNeglected() {
  122. const cause = this.getCause();
  123. return cause instanceof ClientError
  124. || isProtobufMessage(cause) && cause.constructor.typeUrl() === SpineBaseError.typeUrl();
  125. }
  126. }
  127. /**
  128. * An error which occurs when sending off a command to Spine server endpoint results
  129. * with a response indicating that a command message was rejected further processing
  130. * because of a validation error.
  131. *
  132. * @extends CommandHandlingError
  133. */
  134. export class CommandValidationError extends CommandHandlingError {
  135. /**
  136. * @param {!string} message the human-readable error message
  137. * @param {!SpineBaseError} error the command validation error
  138. */
  139. constructor(message, error) {
  140. super(message, error);
  141. }
  142. /**
  143. * @return {ValidationError} command validation error
  144. */
  145. validationError() {
  146. return this.getCause().getValidationError();
  147. }
  148. }