Skip to content

Commit

Permalink
Optmize error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LesterLyu committed Dec 10, 2024
1 parent 0290f13 commit 716c721
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 18 deletions.
15 changes: 10 additions & 5 deletions ckanext/udc/error_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="/user/register">create one</a>.
"""

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
Expand All @@ -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]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,26 @@ const ApproveRequest: React.FC = () => {
const [dialog, setDialog] = useState<{ title: string, message: string }>({ title: '', message: '' });
const [loading, setLoading] = useState<boolean>(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]);

Expand Down

0 comments on commit 716c721

Please sign in to comment.