Some fixes for API log error

This commit is contained in:
Clément Le Bihan
2023-11-28 17:55:40 +01:00
parent 7f282e2ec5
commit 00433ee7ba
3 changed files with 16 additions and 11 deletions
+6 -5
View File
@@ -67,7 +67,9 @@ export class ValidationError extends Error {
export default class API {
public static readonly baseUrl =
Platform.OS === 'web' && !process.env.EXPO_PUBLIC_API_URL ? '/api' : process.env.EXPO_PUBLIC_API_URL!;
Platform.OS === 'web' && !process.env.EXPO_PUBLIC_API_URL
? '/api'
: process.env.EXPO_PUBLIC_API_URL!;
public static async fetch(
params: FetchParams,
handle: Pick<Required<HandleParams>, 'raw'>
@@ -114,11 +116,11 @@ export default class API {
}
const handler = handle.handler;
const body = await response.text();
if (!response.ok) {
throw new APIError(response.statusText ?? body, response.status, 'unknownError');
}
try {
const jsonResponse = JSON.parse(body);
if (!response.ok) {
throw new APIError(response.statusText ?? body, response.status, 'unknownError');
}
const validated = await handler.validator.validate(jsonResponse).catch((e) => {
if (e instanceof yup.ValidationError) {
console.error(e, 'Got: ' + body);
@@ -151,7 +153,6 @@ export default class API {
/// We want that 401 error to be thrown, instead of the plain validation vone
if (e.status == 401)
throw new APIError('invalidCredentials', 401, 'invalidCredentials');
if (!(e instanceof APIError)) throw e;
throw e;
});
}