@@ -40,25 +40,62 @@ export const Code = {
4040
4141/** An error returned by a DataConnect operation. */
4242export class DataConnectError extends FirebaseError {
43- /** The stack of the error. */
44- readonly stack ? : string ;
43+ /** @internal */
44+ readonly name : string = 'DataConnectError' ;
4545
4646 /** @hideconstructor */
47- constructor (
48- /**
49- * The backend error code associated with this error.
50- */
51- readonly code : DataConnectErrorCode ,
52- /**
53- * A custom error description.
54- */
55- readonly message : string
56- ) {
47+ constructor ( code : Code , message : string ) {
5748 super ( code , message ) ;
5849
59- // HACK: We write a toString property directly because Error is not a real
60- // class and so inheritance does not work correctly. We could alternatively
61- // do the same "back-door inheritance" trick that FirebaseError does.
62- this . toString = ( ) => ` ${ this . name } : [code= ${ this . code } ]: ${ this . message } ` ;
50+ // Ensure the instanceof operator works as expected on subclasses of Error.
51+ // See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#custom_error_types
52+ // and https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html#support-for-newtarget
53+ Object . setPrototypeOf ( this , DataConnectError . prototype ) ;
6354 }
55+
56+ /** @internal */
57+ toString ( ) : string {
58+ return `${ this . name } [code=${ this . code } ]: ${ this . message } ` ;
59+ }
60+ }
61+
62+ /** An error returned by a DataConnect operation. */
63+ export class DataConnectOperationError extends DataConnectError {
64+ /** @internal */
65+ readonly name : string = 'DataConnectOperationError' ;
66+
67+ /** The response received from the backend. */
68+ readonly response : DataConnectOperationFailureResponse ;
69+
70+ /** @hideconstructor */
71+ constructor ( message : string , response : DataConnectOperationFailureResponse ) {
72+ super ( Code . PARTIAL_ERROR , message ) ;
73+ this . response = response ;
74+ }
75+ }
76+
77+ export interface DataConnectOperationFailureResponse {
78+ // The "data" provided by the backend in the response message.
79+ //
80+ // Will be `undefined` if no "data" was provided in the response message.
81+ // Otherwise, will be `null` if `null` was explicitly specified as the "data"
82+ // in the response message. Otherwise, will be the value of the "data"
83+ // specified as the "data" in the response message
84+ readonly data ?: Record < string , unknown > | null ;
85+
86+ // The list of errors provided by the backend in the response message.
87+ readonly errors : DataConnectOperationFailureResponseErrorInfo [ ] ;
88+ }
89+
90+ // Information about the error, as provided in the response from the backend.
91+ // See https://spec.graphql.org/draft/#sec-Errors
92+ export interface DataConnectOperationFailureResponseErrorInfo {
93+ // The error message.
94+ readonly message : string ;
95+
96+ // The path of the field in the response data to which this error relates.
97+ // String values in this array refer to field names. Numeric values in this
98+ // array always satisfy `Number.isInteger()` and refer to the index in an
99+ // array.
100+ readonly path : Array < string | number > ;
64101}
0 commit comments