Working validation from the server using CallOuts

This commit is contained in:
Belen Curcio
2018-08-16 16:21:52 -03:00
parent e4ebb2553f
commit 94ad23ce9b
10 changed files with 50 additions and 31 deletions
@@ -0,0 +1,4 @@
export interface ExceptionError {
message: string;
stack: string;
}
@@ -2,4 +2,6 @@ export { default as NetworkError } from "./networkError";
export { default as UnknownServerError } from "./unknownServerError";
export { default as BadUserInputError } from "./badUserInputError";
export { default as GraphQLError } from "./graphqlError";
export { default as ExceptionError } from "./exceptionError";
export * from "./graphqlError";
+8 -6
View File
@@ -18,6 +18,11 @@ const buildOptions = (inputOptions: RequestInit = {}) => {
};
const handleResp = async (res: Response) => {
if (res.status === 404) {
const response = await res.text();
throw new Error(response);
}
if (!res.ok) {
const response = await res.json();
throw new Error(response.error);
@@ -41,7 +46,7 @@ export class RestClient {
this.tokenGetter = tokenGetter;
}
public fetch<T>(path: string, options: PartialRequestInit): Promise<T> {
public async fetch<T>(path: string, options: PartialRequestInit): Promise<T> {
let opts = options;
if (this.tokenGetter) {
opts = merge({}, options, {
@@ -50,10 +55,7 @@ export class RestClient {
},
});
}
return fetch(`${this.uri}${path}`, buildOptions(opts))
.then(handleResp)
.catch(err => {
throw Error(err);
});
const response = await fetch(`${this.uri}${path}`, buildOptions(opts));
return handleResp(response);
}
}