From 716c7211b1b40494b00348b21107c0d711da487e Mon Sep 17 00:00:00 2001 From: LesterLyu Date: Tue, 10 Dec 2024 14:43:17 -0500 Subject: [PATCH] Optmize error handling --- ckanext/udc/error_handler.py | 15 ++++++--- .../src/requestOrgAccess/ApproveRequest.tsx | 32 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/ckanext/udc/error_handler.py b/ckanext/udc/error_handler.py index 9e34863..653995e 100644 --- a/ckanext/udc/error_handler.py +++ b/ckanext/udc/error_handler.py @@ -33,11 +33,14 @@ You are not authorized to request access to an organization. Please login to request access to an organization. If you do not have an account, please create one. """ -messages = [ - MESSAGE_NOT_LOGGED_IN_ADD_TO_CATALOGUE, - MESSAGE_NOT_LOGGED_IN_CREATE_ORGANIZATION, - MESSAGE_NOT_LOGGED_IN_REQUEST_ORGANIZATION_ACCESS, -] +MESSAGE_NOT_LOGGED_IN_WRONG_USER = """ +You are not authorized to access this page. Please login with the correct account. +""" + +MESSAGE_NOT_LOGGED_IN_VIEW_ORGANIZATION_ACCESS_REQUESTS = """ +You are not authorized to view organization access requests. Please login to view organization access requests. +""" + def clear_and_flash(message, category): # Remove the error flash message @@ -53,6 +56,8 @@ def override_error_handler(app: CKANApp, config: CKANConfig): def display_flashes(): if request.full_path.endswith(f"came_from=/{UDC_REACT_PATH}/request-organization-access"): clear_and_flash(MESSAGE_NOT_LOGGED_IN_REQUEST_ORGANIZATION_ACCESS, "alert-warning") + elif f"came_from=/{UDC_REACT_PATH}/request-organization-access/token/" in request.full_path: + clear_and_flash(MESSAGE_NOT_LOGGED_IN_VIEW_ORGANIZATION_ACCESS_REQUESTS, "alert-warning") @app.errorhandler(Forbidden) def handle_forbidden(e) -> Union[tuple[str, Optional[int]], Optional[Response]]: diff --git a/ckanext/udc_react/ckan-udc-react/src/requestOrgAccess/ApproveRequest.tsx b/ckanext/udc_react/ckan-udc-react/src/requestOrgAccess/ApproveRequest.tsx index 3622e47..79e7edb 100644 --- a/ckanext/udc_react/ckan-udc-react/src/requestOrgAccess/ApproveRequest.tsx +++ b/ckanext/udc_react/ckan-udc-react/src/requestOrgAccess/ApproveRequest.tsx @@ -51,20 +51,26 @@ const ApproveRequest: React.FC = () => { const [dialog, setDialog] = useState<{ title: string, message: string }>({ title: '', message: '' }); const [loading, setLoading] = useState(true); - const reload = useCallback(function () { + const reload = useCallback(async function () { if (token) { - executeApiCall(() => api.decodeOrganizationAccessToken(token)).then((data) => { - setData(data); - setLoading(false); - // console.log(data); - }).catch((error) => { - console.error('Failed to fetch user and organization:', error); - if (typeof error === 'string') { - setDialog({ title: 'Error', message: error }); - setShowDialog(true); - } - - }); + const user = await executeApiCall(api.getCurrentUser); + if (user.id == null) { + window.location.href = '/user/login?came_from=' + location.pathname; + } else { + executeApiCall(() => api.decodeOrganizationAccessToken(token)).then((data) => { + setData(data); + setLoading(false); + // console.log(data); + }).catch((error) => { + console.error('Failed to fetch user and organization:', error); + if (typeof error === 'string') { + setDialog({ title: 'Error', message: error }); + setShowDialog(true); + } + + }); + } + } }, [token]);