Skip to content

Commit 9f535fb

Browse files
committed
Implemented partial error
1 parent 8a33180 commit 9f535fb

File tree

2 files changed

+50
-4
lines changed

2 files changed

+50
-4
lines changed

packages/data-connect/src/network/fetch.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { Code, DataConnectError } from '../core/error';
18+
import {
19+
Code,
20+
DataConnectError,
21+
DataConnectOperationError,
22+
DataConnectOperationResponse
23+
} from '../core/error';
1924
import { SDK_VERSION } from '../core/version';
2025
import { logDebug, logError } from '../logger';
2126

@@ -106,10 +111,17 @@ export function dcFetch<T, U>(
106111
return jsonResponse;
107112
})
108113
.then(res => {
114+
console.log(res.errors);
109115
if (res.errors && res.errors.length) {
110116
const stringified = JSON.stringify(res.errors);
111-
logError('DataConnect error while performing request: ' + stringified);
112-
throw new DataConnectError(Code.OTHER, stringified);
117+
const response: DataConnectOperationResponse = {
118+
errors: res.errors,
119+
data: res.data
120+
};
121+
throw new DataConnectOperationError(
122+
'DataConnect error while performing request: ' + stringified,
123+
response
124+
);
113125
}
114126
return res;
115127
});

packages/data-connect/test/unit/fetch.test.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ function mockFetch(json: object, reject: boolean): sinon.SinonStub {
3434
initializeFetch(fakeFetchImpl);
3535
return fakeFetchImpl;
3636
}
37-
describe('fetch', () => {
37+
describe.only('fetch', () => {
3838
it('should throw an error with just the message when the server responds with an error with a message property in the body', async () => {
3939
const message = 'Failed to connect to Postgres instance';
4040
mockFetch(
@@ -85,6 +85,40 @@ describe('fetch', () => {
8585
)
8686
).to.eventually.be.rejectedWith(JSON.stringify(json));
8787
});
88+
it.only('should throw a stringified message when the server responds with an error without a message property in the body', async () => {
89+
const json = {
90+
'data': { 'abc': 'def' },
91+
'errors': [
92+
{
93+
'message':
94+
'SQL query error: pq: duplicate key value violates unique constraint movie_pkey',
95+
'locations': [],
96+
'path': ['the_matrix'],
97+
'extensions': null
98+
}
99+
]
100+
};
101+
mockFetch(json, false);
102+
await expect(
103+
dcFetch(
104+
'http://localhost',
105+
{
106+
name: 'n',
107+
operationName: 'n',
108+
variables: {}
109+
},
110+
{} as AbortController,
111+
null,
112+
null,
113+
null,
114+
false,
115+
CallerSdkTypeEnum.Base
116+
)
117+
).to.eventually.be.rejected.then(error => {
118+
expect(error.response.data).to.eq(json.data);
119+
expect(error.response.errors).to.eq(json.errors);
120+
});
121+
});
88122
it('should assign different values to custom headers based on the _callerSdkType argument (_isUsingGen is false)', async () => {
89123
const json = {
90124
code: 200,

0 commit comments

Comments
 (0)