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 3 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
13 changes: 7 additions & 6 deletions src/application/router/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,20 @@ const routes: RouteRecordRaw[] = [
},

/**
* 404 page
* error page
*/
{
path: '/:pathMatch(.*)*',
path: '/error',
component: ErrorPage,
meta: {
layout: 'fullpage',
pageTitleI18n: 'pages.notFound',
pageTitleI18n: 'pages.error',
discardTabOnLeave: true,
},
props: {
code: 404,
},
props: route => ({
code: route.query.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

}),
Copy link
Contributor

Choose a reason for hiding this comment

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

404 page was actually needed (this was a syntax for each unexpected url, that would show 404 page not found)

we need to create separate /error page, that should have statusCode property and show different info by different status code

Copy link
Contributor

Choose a reason for hiding this comment

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

also better to use route.params for code prop

},
];

Expand Down
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?code=404');
} else {
void router.push('/error');
}
}
}

/**
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 some resource is not found
*/
e11sy marked this conversation as resolved.
Show resolved Hide resolved
export default class ApiError extends DomainError {
/**
* 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