Skip to content

Commit

Permalink
[PLA-1740] bad request 400 error handling (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
zlayine authored Apr 26, 2024
1 parent 2caac24 commit efa4f31
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 50 deletions.
56 changes: 33 additions & 23 deletions resources/js/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useAppStore } from '~/store';
import { HttpMethods } from '~/types/types.enums';
import mutations from '~/api/mutations';
import snackbar from '~/util/snackbar';
import { useAppStore } from '~/store';

export class ApiService {
static async reloadCsrf() {
Expand Down Expand Up @@ -68,7 +68,9 @@ export class ApiService {
}
}

if (resp.status === 204) return null;
if (resp.status === 204) {
return null;
}

if (resp.status === 401) {
useAppStore().clearLogin();
Expand All @@ -77,7 +79,7 @@ export class ApiService {
}

if (resp.status >= 400 && resp.status < 600) {
throw resp as unknown as Error;
throw resp;
}

return resp.json();
Expand All @@ -95,29 +97,37 @@ export class ApiService {
url: `${appStore.config.url}graphql${schema}`,
data,
credentials: appStore.isMultiTenant ? 'include' : 'omit',
}).then((res: any) => {
if (res.errors) {
const message = res.errors[0].message;
if (message === 'validation') {
const error = res.errors[0].extensions?.validation;
const errors = Object.keys(error).map((key) => {
return {
field: key,
message: error[key][0],
};
})
.then((res) => {
resolve(res);
})
.catch((error) => {
if (error instanceof Response) {
error.json().then((res) => {
if (res.errors) {
const message = res.errors[0].message;
if (message === 'validation') {
const error = res.errors[0].extensions?.validation;
const errors = Object.keys(error).map((key) => {
return {
field: key,
message: error[key][0],
};
});
reject(errors);
} else {
reject({ field: 'Error', message });
}
}

if (res.field == 'Error') {
reject(res);
}
});
reject(errors);
} else {
reject({ field: 'Error', message });
reject({ field: 'Error', message: 'Unknown error' });
}
}

if (res.field == 'Error') {
reject(res);
}

resolve(res);
});
});
});
}

Expand Down
4 changes: 3 additions & 1 deletion resources/js/components/batch/BatchMint.vue
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@
>
Cancel
</RouterLink>
<Btn dusk="submitBtn" :loading="isLoading" :disabled="isLoading" primary is-submit>Batch Mint</Btn>
<Btn dusk="submitBtn" :loading="isLoading" :disabled="isLoading" primary is-submit
>Batch Mint</Btn
>
</div>
</div>
</Form>
Expand Down
6 changes: 5 additions & 1 deletion resources/js/components/pages/Collections.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@
class="inline-block min-w-full py-2 align-middle sm:px-6 lg:px-8"
:is-loading="isLoading"
>
<table id="collectionsTable" class="min-w-full divide-y divide-gray-300" v-if="collections.items?.length">
<table
id="collectionsTable"
class="min-w-full divide-y divide-gray-300"
v-if="collections.items?.length"
>
<thead>
<tr>
<th
Expand Down
11 changes: 9 additions & 2 deletions resources/js/components/pages/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,12 @@
</div>
<div class="flex flex-col space-y-8">
<div v-if="appStore.isMultiTenant" class="flex flex-col space-y-4">
<CollapseCard dusk-id="apiTokensTab" title="Api tokens" :actions="false" :isOpen="!appStore.hasValidConfig">
<CollapseCard
dusk-id="apiTokensTab"
title="Api tokens"
:actions="false"
:isOpen="!appStore.hasValidConfig"
>
<div>
<SettingsApiTokens />
</div>
Expand Down Expand Up @@ -56,7 +61,9 @@
readmore="Advanced mode"
/>
<div class="flex space-x-4">
<Btn dusk="deleteBtn" v-if="appStore.isMultiTenant" @click="confirmModal = true">Delete account</Btn>
<Btn dusk="deleteBtn" v-if="appStore.isMultiTenant" @click="confirmModal = true"
>Delete account</Btn
>
<Btn dusk="logoutBtn" v-if="appStore.isMultiTenant" class="mr-auto" @click="logout">Logout</Btn>
<Btn dusk="resetBtn" v-else class="mr-auto" error @click="resetSettings">Reset Settings</Btn>
</div>
Expand Down
8 changes: 7 additions & 1 deletion resources/js/components/pages/SettingsResetPassword.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
<template>
<div>
<div v-if="enableReset" class="flex flex-col space-y-4">
<Form id="passwordResetForm" ref="formRef" class="space-y-6" :validation-schema="validation" @submit="resetPassword">
<Form
id="passwordResetForm"
ref="formRef"
class="space-y-6"
:validation-schema="validation"
@submit="resetPassword"
>
<FormInput
v-model="oldPassword"
label="Old Password"
Expand Down
20 changes: 13 additions & 7 deletions resources/js/components/pages/auth/RequestResetPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,20 @@ const requestReset = async (recaptcha?: string) => {
isLoading.value = true;
try {
await AuthApi.requestPasswordReset(email.value, recaptcha);
} catch {
// do nothing
snackbar.success({
title: "If an account with this email exists, you'll receive an email shortly with information on resetting your password.",
save: false,
});
} catch (e: any) {
if (e.message.includes('Too many requests')) {
snackbar.error({
title: 'Error',
text: e.message,
});
}
} finally {
isLoading.value = false;
}
snackbar.success({
title: "If an account with this email exists, you'll receive an email shortly with information on resetting your password.",
save: false,
});
isLoading.value = false;
};
(async () => {
Expand Down
11 changes: 3 additions & 8 deletions resources/js/components/pages/auth/ResetPassword.vue
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,10 @@ const resetPassword = async () => {
snackbar.error({ title: 'Invalid user', text: 'Please check the email provided' });
}
} catch (e: any) {
if (snackbarErrors(e)) return;
if (e.message.includes('Too many requests')) {
snackbar.error({
title: 'Too many requests',
text: e.message,
});
} else {
snackbar.error({ title: 'Error resetting password.' });
if (snackbarErrors(e)) {
return;
}
snackbar.error({ title: 'Error resetting password.' });
} finally {
isLoading.value = false;
}
Expand Down
4 changes: 2 additions & 2 deletions resources/js/graphql/mutation/auth/RequestPasswordReset.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export default `mutation RequestPasswordReset($email: String!, $recaptcha: String!) {
RequestPasswordReset(email: $email, recaptchaResponse: $recaptcha)
export default `mutation RequestPasswordReset($email: String!) {
RequestPasswordReset(email: $email)
}`;
10 changes: 5 additions & 5 deletions resources/js/store/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { defineStore } from 'pinia';
import { AppState } from '~/types/types.interface';
import appConfig from '~/config.json';
import { ApiService } from '~/api';
import snackbar from '~/util/snackbar';
import { AppState } from '~/types/types.interface';
import { AuthApi } from '~/api/auth';
import { CollectionApi } from '~/api/collection';
import appConfig from '~/config.json';
import { defineStore } from 'pinia';
import snackbar from '~/util/snackbar';
import { useConnectionStore } from './connection';

const parseConfigURL = (url: string): URL => {
Expand Down Expand Up @@ -56,7 +56,7 @@ export const useAppStore = defineStore('app', {
}

const urlConfig = await this.checkURL(this.config.url);
this.config.network = urlConfig.network;
this.config.network = urlConfig?.network;
this.config.packages = Object.entries(urlConfig.packages).map(([key, value]: any[]) => {
let link =
urlConfig.url +
Expand Down

0 comments on commit efa4f31

Please sign in to comment.