Skip to content

Commit

Permalink
feat(treat-unhandled-rejection): backs again to the global event handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cristiantela committed Oct 15, 2024
1 parent 776b770 commit 9defc14
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 0 additions & 4 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,10 @@
</template>

<script setup lang="ts">
import { onErrorCaptured } from 'vue';
import TestTranslations from '@/components/TestTranslations.vue';
import { useAlertStore } from './stores/Alert';
import { useAuthStore } from './stores/Auth';
import { useI18n } from 'vue-i18n';
import { onUnhandledRejection } from '@/utils/TreatUnhandledRejection';
onErrorCaptured(onUnhandledRejection);
const { locale } = useI18n();
const alertStore = useAlertStore();
Expand Down
1 change: 1 addition & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import './assets/main.css';
import '@/utils/TreatUnhandledRejection';

import { createApp } from 'vue';
import { createPinia } from 'pinia';
Expand Down
14 changes: 12 additions & 2 deletions src/utils/TreatUnhandledRejection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@ import { useAlertStore } from '@/stores/Alert';
import { AxiosError, HttpStatusCode } from 'axios';
import { get } from 'lodash';

export const onUnhandledRejection = (error: Error | AxiosError) => {
export const onUnhandledRejection = ({
reason: error,
}: Pick<PromiseRejectionEvent, 'reason'>) => {
const alertStore = useAlertStore();

if (error instanceof AxiosError) {
const errorMessage = get(error, 'response.data.error');
const statusCode = get(error, 'response.status');

if (statusCode === HttpStatusCode.Unauthorized) {
if (errorMessage && typeof errorMessage === 'string') {
alertStore.add({
type: 'error',
text: errorMessage,
});
} else if (statusCode === HttpStatusCode.Unauthorized) {
alertStore.add({
type: 'error',
text: i18n.global.t('common.errors.unauthorized'),
Expand All @@ -30,3 +38,5 @@ export const onUnhandledRejection = (error: Error | AxiosError) => {
}
}
};

window.addEventListener('unhandledrejection', onUnhandledRejection);
21 changes: 18 additions & 3 deletions src/utils/__tests__/TreatUnhandledRejection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,31 @@ describe('TreatUnhandledRejection', () => {
])(
'Axios status $status should alert $alertText',
({ status, alertText }) => {
onUnhandledRejection(
new AxiosError('Error', AxiosError.ERR_BAD_RESPONSE, {}, null, {
onUnhandledRejection({
reason: new AxiosError('Error', AxiosError.ERR_BAD_RESPONSE, {}, null, {
status,
}),
);
});

expect(alertStore.add).toHaveBeenCalledWith({
type: 'error',
text: alertText,
});
},
);

describe('when error is Axios and it has a response error string', () => {
it('should alert the response error', () => {
onUnhandledRejection({
reason: new AxiosError('Error', AxiosError.ERR_BAD_RESPONSE, {}, null, {
data: { error: 'Error message' },
}),
});

expect(alertStore.add).toHaveBeenCalledWith({
type: 'error',
text: 'Error message',
});
});
});
});

0 comments on commit 9defc14

Please sign in to comment.