relay-runtime 0.0.0-main-b6199194 → 0.0.0-main-042ceca3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,19 +11,17 @@
11
11
 
12
12
  'use strict';
13
13
 
14
- import type {TRelayFieldErrorForDisplay} from '../store/RelayErrorTrie';
15
14
  import type {
15
+ ErrorResponseField,
16
16
  ErrorResponseFields,
17
17
  IEnvironment,
18
- MissingRequiredFields,
19
18
  } from '../store/RelayStoreTypes';
20
19
 
21
- const {RelayFieldError} = require('../store/RelayErrorTrie');
20
+ const invariant = require('invariant');
22
21
 
23
22
  function handleFieldErrors(
24
23
  environment: IEnvironment,
25
24
  errorResponseFields: ErrorResponseFields,
26
- shouldThrow: boolean,
27
25
  ) {
28
26
  for (const fieldError of errorResponseFields) {
29
27
  // First we log all events. Note that the logger may opt to throw its own
@@ -32,79 +30,66 @@ function handleFieldErrors(
32
30
  environment.relayFieldLogger(fieldError);
33
31
  }
34
32
 
35
- // when a user adds the throwOnFieldError flag, they opt into also throwing on missing fields.
36
- if (shouldThrow) {
37
- throw new RelayFieldError(
38
- `Relay: Unexpected response payload - this object includes an errors property in which you can access the underlying errors`,
39
- errorResponseFields.map((event): TRelayFieldErrorForDisplay => {
40
- switch (event.kind) {
41
- case 'relay_field_payload.error':
42
- //TODO: [relay] Provide a payloadErrorResolver to allow exposing custom error shape.
43
- const {message, ...displayError} = event.error;
44
- return displayError;
45
- case 'missing_expected_data.throw':
46
- return {path: event.fieldPath.split('.')};
47
- case 'missing_expected_data.log':
48
- return {path: event.fieldPath.split('.')};
49
- case 'relay_resolver.error':
50
- return {path: event.fieldPath.split('.')};
51
- default:
52
- (event.kind: empty);
53
- throw new Error('Relay: Unexpected event kind');
54
- }
55
- }),
56
- );
33
+ for (const fieldError of errorResponseFields) {
34
+ if (eventShouldThrow(fieldError)) {
35
+ switch (fieldError.kind) {
36
+ case 'relay_resolver.error':
37
+ throw new Error(
38
+ `Relay: Resolver error at path '${fieldError.fieldPath}' in '${fieldError.owner}'.`,
39
+ );
40
+ case 'relay_field_payload.error':
41
+ throw new Error(
42
+ `Relay: Unexpected response payload - this object includes an errors property in which you can access the underlying errors`,
43
+ );
44
+ case 'missing_expected_data.throw':
45
+ throw new Error(
46
+ `Relay: Missing expected data at path '${fieldError.fieldPath}' in '${fieldError.owner}'.`,
47
+ );
48
+ case 'missing_required_field.throw':
49
+ throw new Error(
50
+ `Relay: Missing @required value at path '${fieldError.fieldPath}' in '${fieldError.owner}'.`,
51
+ );
52
+ case 'missing_required_field.log':
53
+ case 'missing_expected_data.log':
54
+ // These should have already been filtered out. Sadly, Flow Type
55
+ // Guards don't work well with refining discriminated unions, so we
56
+ // can't assert this via types.
57
+ break;
58
+ default:
59
+ (fieldError.kind: empty);
60
+ invariant(false, 'Relay: Unexpected event kind: %s', fieldError.kind);
61
+ }
62
+ }
57
63
  }
58
64
  }
59
65
 
60
- function handleMissingRequiredFields(
61
- environment: IEnvironment,
62
- missingRequiredFields: MissingRequiredFields,
63
- ) {
64
- switch (missingRequiredFields.action) {
65
- case 'THROW': {
66
- const {path, owner} = missingRequiredFields.field;
67
- // This gives the consumer the chance to throw their own error if they so wish.
68
- environment.relayFieldLogger({
69
- kind: 'missing_required_field.throw',
70
- owner,
71
- fieldPath: path,
72
- });
73
- throw new Error(
74
- `Relay: Missing @required value at path '${path}' in '${owner}'.`,
75
- );
76
- }
77
- case 'LOG':
78
- missingRequiredFields.fields.forEach(({path, owner}) => {
79
- environment.relayFieldLogger({
80
- kind: 'missing_required_field.log',
81
- owner,
82
- fieldPath: path,
83
- });
84
- });
85
- break;
86
- default: {
87
- (missingRequiredFields.action: empty);
88
- }
66
+ function eventShouldThrow(event: ErrorResponseField): boolean {
67
+ switch (event.kind) {
68
+ case 'relay_resolver.error':
69
+ case 'relay_field_payload.error':
70
+ return event.shouldThrow && !event.handled;
71
+ case 'missing_expected_data.throw':
72
+ case 'missing_required_field.throw':
73
+ return !event.handled;
74
+ case 'missing_required_field.log':
75
+ case 'missing_expected_data.log':
76
+ return false;
77
+ default:
78
+ (event.kind: empty);
79
+ throw new Error('Relay: Unexpected event kind');
89
80
  }
90
81
  }
91
82
 
92
83
  function handlePotentialSnapshotErrors(
93
84
  environment: IEnvironment,
94
- missingRequiredFields: ?MissingRequiredFields,
95
85
  errorResponseFields: ?ErrorResponseFields,
96
- throwOnFieldError: boolean,
97
86
  ) {
98
- if (missingRequiredFields != null) {
99
- handleMissingRequiredFields(environment, missingRequiredFields);
100
- }
101
-
102
87
  /**
103
88
  * Inside handleFieldErrors, we check for throwOnFieldError - but this fn logs the error anyway by default
104
89
  * which is why this still should run in any case there's errors.
105
90
  */
106
91
  if (errorResponseFields != null) {
107
- handleFieldErrors(environment, errorResponseFields, throwOnFieldError);
92
+ handleFieldErrors(environment, errorResponseFields);
108
93
  }
109
94
  }
110
95