From 1ea939f41fb3cee198c60a53c69f4bab25d7e910 Mon Sep 17 00:00:00 2001 From: vchikalkin Date: Thu, 6 Jun 2024 12:36:07 +0300 Subject: [PATCH] pass error from telegram server to client --- apps/api/src/ldap-tfa/ldap-tfa.controller.ts | 2 +- apps/web/components/Form.tsx | 19 +++++++++++++------ apps/web/types/error.ts | 3 +++ 3 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 apps/web/types/error.ts diff --git a/apps/api/src/ldap-tfa/ldap-tfa.controller.ts b/apps/api/src/ldap-tfa/ldap-tfa.controller.ts index c9004c4..401526d 100644 --- a/apps/api/src/ldap-tfa/ldap-tfa.controller.ts +++ b/apps/api/src/ldap-tfa/ldap-tfa.controller.ts @@ -79,7 +79,7 @@ export class LdapTfaController extends LdapController { }, }) .then((res) => reply.status(200).send(res.data)) - .catch((error) => reply.status(500).send(error)); + .catch((error) => reply.status(500).send(error.response.data)); } @Get('/telegram-confirm') diff --git a/apps/web/components/Form.tsx b/apps/web/components/Form.tsx index b70217c..52950ed 100644 --- a/apps/web/components/Form.tsx +++ b/apps/web/components/Form.tsx @@ -5,8 +5,9 @@ import styles from './Form.module.scss'; import { publicRuntimeConfig } from '@/config/runtime'; import { FormStateContext } from '@/context/form-state'; import { useSocket } from '@/hooks/socket'; +import type { TelegramUrlResponse } from '@/types/error'; import type { LdapUser } from '@/types/user'; -import axios from 'axios'; +import axios, { isAxiosError } from 'axios'; import Image from 'next/image'; import type { PropsWithChildren } from 'react'; import { useContext, useEffect } from 'react'; @@ -157,12 +158,18 @@ export const Form = { type: 'set-step', }); }) - .catch(() => - dispatch({ - payload: { error: ERROR_SERVER }, + .catch((error_) => { + let error = ERROR_SERVER; + + if (isAxiosError(error_) && error_.response?.data?.message) { + error = error_.response?.data?.message; + } + + return dispatch({ + payload: { error }, type: 'set-error', - }) - ); + }); + }); } if (step === 'telegram') { diff --git a/apps/web/types/error.ts b/apps/web/types/error.ts new file mode 100644 index 0000000..4376bfe --- /dev/null +++ b/apps/web/types/error.ts @@ -0,0 +1,3 @@ +export type TelegramUrlResponse = { + message: string; +};