Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Redirect to 404 Page When Note is Not Found #277

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/application/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@
"noteSettings": "Note settings",
"marketplace": "Marketplace",
"addTool": "Add tool",
"error": "Error",
"notFound": "Not found",
"joinTeam": "Join",
"authorization": "Authorize",
Expand Down
17 changes: 16 additions & 1 deletion src/application/router/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,6 @@ const routes: RouteRecordRaw[] = [
discardTabOnLeave: true,
},
},

/**
* 404 page
*/
Expand All @@ -195,6 +194,22 @@ const routes: RouteRecordRaw[] = [
code: 404,
},
},
/**
* error page
*/
{
path: '/error/:code',
component: ErrorPage,
meta: {
layout: 'fullpage',
pageTitleI18n: 'pages.error',
discardTabOnLeave: true,
},
props: route => ({
code: route.params.code,
customMessage: route.query.message,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont see where ?message is composed. And it's better to use a prop instead of adding a message to the URL.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you want 'page not found' message right, so on error component it is getting that message from i18n based on error code, if you want custom message then we can pass it as query

}),
},
];

export default routes;
21 changes: 15 additions & 6 deletions src/application/services/useNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { NoteTool } from '@/domain/entities/Note';
import { useRouter, useRoute } from 'vue-router';
import type { NoteDraft } from '@/domain/entities/NoteDraft';
import type EditorTool from '@/domain/entities/EditorTool';
import NotFoundError from '@/domain/entities/errors/NotFound';
import useHeader from './useHeader';
import { getTitle } from '@/infrastructure/utils/note';

Expand Down Expand Up @@ -166,12 +167,20 @@ export default function (options: UseNoteComposableOptions): UseNoteComposableSt
/**
* @todo try-catch domain errors
*/
const response = await noteService.getNoteById(id);

note.value = response.note;
canEdit.value = response.accessRights.canEdit;
noteTools.value = response.tools;
parentNote.value = response.parentNote;
try {
const response = await noteService.getNoteById(id);

note.value = response.note;
canEdit.value = response.accessRights.canEdit;
noteTools.value = response.tools;
parentNote.value = response.parentNote;
} catch (error) {
if (error instanceof NotFoundError) {
void router.push(`/error/404?message=${error.message}`);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's better to specify error message directly, instead of using error.message. And use i18n for that.

Other code looks good for me

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have removed that custom message, now it will get message based on code , in this case it will be 'Page not found'(i18n), is it ok

} else {
void router.push('/error/500');
}
}
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/domain/entities/errors/ApiError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import DomainError from './Base';

/**
* Domain error thrown when unknown error occurs
*/
export default class ApiError extends DomainError {
/**
Comment on lines +3 to +7
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think, that DomainError actually extends ApiError

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, currently it is not needed, I had created the ApiError to consider it as default error, to check api error response is instance of ApiError or not,

* Constructor for ApiError error
* @param message - Error message
* @param statusCode - Error status code
*/
constructor(message: string, public statusCode: number) {
super(message);
this.name = 'ApiError';
}
}
3 changes: 2 additions & 1 deletion src/infrastructure/transport/notes-api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import AuthorizableTransport from '@/infrastructure/transport/authorizable.trans
import type JSONValue from '../types/JSONValue';
import UnauthorizedError from '@/domain/entities/errors/Unauthorized';
import NotFoundError from '@/domain/entities/errors/NotFound';
import ApiError from '@/domain/entities/errors/ApiError';
import type { FilesDto } from '../types/FileDto';

/**
Expand Down Expand Up @@ -53,7 +54,7 @@ export default class NotesApiTransport extends AuthorizableTransport {
case 404:
return new NotFoundError(errorText);
default:
return new Error(errorText);
return new ApiError(errorText, status);
}
},
});
Expand Down
17 changes: 17 additions & 0 deletions src/presentation/pages/Error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@
<script setup lang="ts">
import { computed } from 'vue';
import { useI18n } from 'vue-i18n';
import { useHead } from 'unhead';
import { useRoute } from 'vue-router';
import useHeader from '@/application/services/useHeader';

const route = useRoute();
const { patchOpenedPageByUrl } = useHeader();

const { t, te } = useI18n();

Expand Down Expand Up @@ -44,6 +50,17 @@ const message = computed(() => {

return t('errors.default');
});

useHead({
title: t(`errors.${props.code}`),
});

const openPageInfo = {
title: t(`errors.${props.code}`),
url: route.path,
};

patchOpenedPageByUrl(route.path, openPageInfo);
</script>

<style lang="postcss" module>
Expand Down
Loading