internalErrorClasses.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import { GraphQLError } from 'graphql';
  2. import { ApolloServerErrorCode } from './errors/index.js';
  3. import { newHTTPGraphQLHead } from './runHttpQuery.js';
  4. import { HeaderMap } from './utils/HeaderMap.js';
  5. class GraphQLErrorWithCode extends GraphQLError {
  6. constructor(message, code, options) {
  7. super(message, {
  8. ...options,
  9. extensions: { ...options?.extensions, code },
  10. });
  11. this.name = this.constructor.name;
  12. }
  13. }
  14. export class SyntaxError extends GraphQLErrorWithCode {
  15. constructor(graphqlError) {
  16. super(graphqlError.message, ApolloServerErrorCode.GRAPHQL_PARSE_FAILED, {
  17. source: graphqlError.source,
  18. positions: graphqlError.positions,
  19. extensions: { http: newHTTPGraphQLHead(400), ...graphqlError.extensions },
  20. originalError: graphqlError,
  21. });
  22. }
  23. }
  24. export class ValidationError extends GraphQLErrorWithCode {
  25. constructor(graphqlError) {
  26. super(graphqlError.message, ApolloServerErrorCode.GRAPHQL_VALIDATION_FAILED, {
  27. nodes: graphqlError.nodes,
  28. extensions: {
  29. http: newHTTPGraphQLHead(400),
  30. ...graphqlError.extensions,
  31. },
  32. originalError: graphqlError.originalError ?? graphqlError,
  33. });
  34. }
  35. }
  36. const getPersistedQueryErrorHttp = () => ({
  37. status: 200,
  38. headers: new HeaderMap([
  39. ['cache-control', 'private, no-cache, must-revalidate'],
  40. ]),
  41. });
  42. export class PersistedQueryNotFoundError extends GraphQLErrorWithCode {
  43. constructor() {
  44. super('PersistedQueryNotFound', ApolloServerErrorCode.PERSISTED_QUERY_NOT_FOUND, { extensions: { http: getPersistedQueryErrorHttp() } });
  45. }
  46. }
  47. export class PersistedQueryNotSupportedError extends GraphQLErrorWithCode {
  48. constructor() {
  49. super('PersistedQueryNotSupported', ApolloServerErrorCode.PERSISTED_QUERY_NOT_SUPPORTED, { extensions: { http: getPersistedQueryErrorHttp() } });
  50. }
  51. }
  52. export class UserInputError extends GraphQLErrorWithCode {
  53. constructor(graphqlError) {
  54. super(graphqlError.message, ApolloServerErrorCode.BAD_USER_INPUT, {
  55. nodes: graphqlError.nodes,
  56. originalError: graphqlError.originalError ?? graphqlError,
  57. extensions: graphqlError.extensions,
  58. });
  59. }
  60. }
  61. export class OperationResolutionError extends GraphQLErrorWithCode {
  62. constructor(graphqlError) {
  63. super(graphqlError.message, ApolloServerErrorCode.OPERATION_RESOLUTION_FAILURE, {
  64. nodes: graphqlError.nodes,
  65. originalError: graphqlError.originalError ?? graphqlError,
  66. extensions: {
  67. http: newHTTPGraphQLHead(400),
  68. ...graphqlError.extensions,
  69. },
  70. });
  71. }
  72. }
  73. export class BadRequestError extends GraphQLErrorWithCode {
  74. constructor(message, options) {
  75. super(message, ApolloServerErrorCode.BAD_REQUEST, {
  76. ...options,
  77. extensions: { http: newHTTPGraphQLHead(400), ...options?.extensions },
  78. });
  79. }
  80. }
  81. //# sourceMappingURL=internalErrorClasses.js.map