mirror of
https://github.com/wassname/talk.git
synced 2026-08-19 12:40:16 +08:00
Working validation from the server using CallOuts
This commit is contained in:
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user