Skip to content

Commit 63b0b0d

Browse files
authored
docs:30-dịch-3-basic-sveltekit-errors-and-redirects (sveltevietnam#65)
1 parent a8cc570 commit 63b0b0d

File tree

20 files changed

+61
-60
lines changed

20 files changed

+61
-60
lines changed
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
---
2-
title: Basics
2+
title: Cơ bản
33
---
44

5-
There are two types of errors in SvelteKit — _expected_ errors and _unexpected_ errors.
5+
Trong SvelteKit, có hai loại lỗi — _expected_ errors và _unexpected_ errors.
6+
7+
Expected error là một lỗi được tạo ra bằng helper [`error`](https://kit.svelte.dev/docs/modules#sveltejs-kit-error) từ `@sveltejs/kit`, như trong `src/routes/expected/+page.server.js`:
68

7-
An expected error is one that was created with the [`error`](https://kit.svelte.dev/docs/modules#sveltejs-kit-error) helper from `@sveltejs/kit`, as in `src/routes/expected/+page.server.js`:
89

910
```js
1011
/// file: src/routes/expected/+page.server.js
1112
import { error } from '@sveltejs/kit';
1213

1314
export function load() {
14-
throw error(420, 'Enhance your calm');
15+
throw error(420, 'Bình tĩnh');
1516
}
1617
```
1718

18-
Any other error — such as the one in `src/routes/unexpected/+page.server.js` is treated as unexpected:
19+
Bất kỳ lỗi nào khác — như trong `src/routes/unexpected/+page.server.js` được coi là lỗi unexpected:
1920

2021
```js
2122
/// file: src/routes/unexpected/+page.server.js
@@ -24,8 +25,8 @@ export function load() {
2425
}
2526
```
2627

27-
When you throw an expected error, you're telling SvelteKit 'don't worry, I know what I'm doing here'. An unexpected error, by contrast, is assumed to be a bug in your app. When an unexpected error is thrown, its message and stack trace will be logged to the console.
28+
Khi bạn throw một lỗi expected, bạn đang bảo SvelteKit 'đừng lo, tôi biết tôi đang làm gì ở đây'. Ngược lại, một lỗi unexpected được giả định là một lỗi trong ứng dụng của bạn. Khi một lỗi unexpected xảy ra, message stack trace của nó sẽ được ghi vào console.
2829

29-
> In a later chapter we'll learn about how to add custom error handling using the `handleError` hook.
30+
> Trong chương sau, chúng ta sẽ tìm hiểu về cách sử dụng `handleError` để tùy chỉnh việc xử lý lỗi.
3031
31-
If you click the links in this app, you'll notice an important difference: the expected error message is shown to the user, whereas the unexpected error message is redacted and replaced with a generic 'Internal Error' message and a 500 status code. That's because error messages can contain sensitive data.
32+
Nếu bạn nhấp vào các liên kết trong ứng dụng này, bạn sẽ thấy một khác biệt quan trọng: thông báo lỗi expected được hiển thị cho người dùng, trong khi thông báo của lỗi unexpected được thay thế bằng một thông báo tổng quát là 'Internal Error' và một mã trạng thái 500. Đây là vì thông baó lỗi có thể chứa dữ liệu nhạy cảm.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<nav>
22
<a href="/">home</a>
3-
<a href="/expected">page with expected error</a>
4-
<a href="/unexpected">page with unexpected error</a>
3+
<a href="/expected">trang với expected error</a>
4+
<a href="/unexpected">trang với unexpected error</a>
55
</nav>
66

77
<slot />
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export const emojis = {
2-
// TODO add the rest!
2+
// TODO thêm phần còn lại!
33
420: '🫠',
44
500: '💥'
55
};
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { error } from '@sveltejs/kit';
22

33
export function load() {
4-
throw error(420, 'Enhance your calm');
4+
throw error(420, 'Bình tĩnh');
55
}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>This page will never be rendered</h1>
1+
<h1>Trang này sẽ không bao giờ được render</h1>
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>This page will never be rendered</h1>
1+
<h1>Trang này sẽ không bao giờ được render</h1>

content/tutorial/03-sveltekit/09-errors-and-redirects/02-error-pages/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Error pages
2+
title: Trang lỗi
33
---
44

5-
When something goes wrong inside a `load` function, SvelteKit renders an error page.
5+
Khi xảy ra vấn đề trong hàm `load`, SvelteKit sẽ render một trang lỗi.
66

7-
The default error page is somewhat bland. We can customize it by creating a `src/routes/+error.svelte` component:
7+
Trang lỗi mặc định hơi nhạt nhòa. Chúng ta có thể tùy chỉnh nó bằng cách tạo một component `src/routes/+error.svelte`:
88

99
```svelte
1010
/// file: src/routes/+error.svelte
@@ -19,11 +19,11 @@ The default error page is somewhat bland. We can customize it by creating a `src
1919
</span>
2020
```
2121

22-
Notice that the `+error.svelte` component is rendered inside the root `+layout.svelte`. We can create more granular `+error.svelte` boundaries:
22+
Chú ý component `+error.svelte` được render bên trong root `+layout.svelte`. Chúng ta có thể tạo ra các trang lỗi `+error.svelte` cụ thể cho từng trang:
2323

2424
```svelte
2525
/// file: src/routes/expected/+error.svelte
26-
<h1>this error was expected</h1>
26+
<h1>lỗi expected</h1>
2727
```
2828

29-
This component will be rendered for `/expected`, while the root `src/routes/+error.svelte` page will be rendered for any other errors that occur.
29+
Component này sẽ được render cho `/expected`, trong khi trang gốc `src/routes/+error.svelte` sẽ được render cho bất kỳ lỗi nào khác xảy ra.
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<h1>this error was expected</h1>
1+
<h1>lỗi expected</h1>

content/tutorial/03-sveltekit/09-errors-and-redirects/03-fallback-errors/README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
title: Fallback errors
33
---
44

5-
If things go _really_ wrong — an error occurs while loading the root layout data, or while rendering the error page — SvelteKit will fall back to a static error page.
5+
Khi có lỗi xảy ra trong quá trình tải dữ liệu ở root layout hoăc trong khi render trang lỗi - SvelteKit sẽ chuyển sang một trang lỗi tĩnh.
66

7-
Add a new `src/routes/+layout.server.js` file to see this in action:
7+
Thêm một tệp mới `src/routes/+layout.server.js` để thấy điều này diễn ra:
88

99
```js
1010
/// file: src/routes/+layout.server.js
@@ -13,16 +13,16 @@ export function load() {
1313
}
1414
```
1515

16-
You can customise the fallback error page. Create a `src/error.html` file:
16+
Bạn có thể tùy chỉnh trang fallback error. Tạo một tệp `src/error.html`:
1717

1818
```html
1919
/// file: src/error.html
20-
<h1>Game over</h1>
20+
<h1>Kết thúc</h1>
2121
<p>Code %sveltekit.status%</p>
2222
<p>%sveltekit.error.message%</p>
2323
```
2424

25-
This file can include the following:
25+
Tệp này có thể bao gồm:
2626

27-
- `%sveltekit.status%`the HTTP status code
28-
- `%sveltekit.error.message%`the error message
27+
- `%sveltekit.status%`mã trạng thái HTTP
28+
- `%sveltekit.error.message%`thông báo lỗi
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<h1>Game over</h1>
1+
<h1>Kết thúc</h1>
22
<p>Code %sveltekit.status%</p>
33
<p>%sveltekit.error.message%</p>
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Redirects
2+
title: Chuyển hướng
33
---
44

5-
We can also use the `throw` mechanism to redirect from one page to another.
5+
Chúng ta cũng có thể sử dụng cơ chế `throw` để chuyển hướng từ một trang đến trang khác.
66

7-
Create a new `load` function in `src/routes/a/+page.server.js`:
7+
Tạo một hàm `load` mới trong `src/routes/a/+page.server.js`:
88

99
```js
1010
/// file: src/routes/a/+page.server.js
@@ -15,12 +15,12 @@ export function load() {
1515
}
1616
```
1717

18-
Navigating to `/a` will now take us straight to `/b`.
18+
Việc chuyển đến `/a` sẽ đưa chúng ta thẳng đến `/b`.
1919

20-
You can `throw redirect(...)` inside `load` functions, form actions, API routes and the `handle` hook, which we'll discuss in a later chapter.
20+
Bạn có thể `throw redirect(...)` trong các hàm `load`, form actions, API routes và hook `handle` - chúng ta sẽ thảo luận vấn đề này trong một chương sau.
2121

22-
The most common status codes you'll use:
22+
Các status codes phổ biến nhất mà bạn sẽ sử dụng:
2323

24-
- `303`for form actions, following a successful submission
25-
- `307`for temporary redirects
26-
- `308`for permanent redirects
24+
303 — đối với form actions, sau khi submit thành công
25+
307 — đối với chuyển hướng tạm thời
26+
308 — đối với chuyển hướng vĩnh viễn
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"title": "Errors and redirects"
2+
"title": "Lỗi và chuyển hướng"
33
}
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Customizing the error message
2+
title: Tùy chỉnh thông báo lỗi
33
---
44

5-
The error page in the previous exercise is rather static. Maybe you want to show the error message so you can help people turning up in your support channels faster.
5+
Trang lỗi trong bài tập trước khá tĩnh. Có thể bạn muốn hiển thị thông báo lỗi để giúp người dùng có thể tìm đến các kênh hỗ trợ nhanh hơn.
66

7-
For this, SvelteKit provides you with `$page.error` and `$page.status`, which contain information about the error and the status code. Let's add it to `+error.svelte`:
7+
Đối với điều này, SvelteKit cung cấp cho bạn `$page.error` `$page.status`, chứa thông tin về lỗi và status code _(mã trạng thái)_. Hãy thêm nó vào `+error.svelte`:
88

99
```svelte
1010
/// file: src/routes/+error.svelte
@@ -19,36 +19,36 @@ For this, SvelteKit provides you with `$page.error` and `$page.status`, which co
1919
+++{#if $page.status === 404}
2020
<h1>Not found</h1>
2121
{:else +++if !online}
22-
<h1>You're offline</h1>
22+
<h1>Bạn đang offline</h1>
2323
{:else}
2424
<h1>Oops!</h1>
25-
---<p>Something went wrong</p>---
25+
---<p>Có gì đó sai sai</p>---
2626
+++<p>{$page.error.message}</p>+++
2727
{/if}
2828
```
2929

30-
That's better, but `$page.error.message` always contains "Internal Error" - how so? This is because SvelteKit plays it safe and prevents you from accidentally showing sensitive information as part of the error message.
30+
`$page.error.message` luôn chứa "Internal Error" - tại sao vậy? Điều này là do SvelteKit ưu tiên an toàn và ngăn bạn sơ ý để lộ thông tin nhạy cảm.
3131

32-
To customize it, implement the `handleError` hook in `hooks.server.js` and `hooks.client.js` which run when an unexpected error is thrown during data loads on the server or client respectively.
32+
Để tùy chỉnh, triển khai hook `handleError` trong `hooks.server.js` `hooks.client.js`, được chạy khi một lỗi unexpected xảy ra trong quá trình tải dữ liệu trên server hoặc client.
3333

3434
```js
3535
// hooks.server.js
3636
export function handleError(+++{ error }+++) {
37-
---return { message: 'Internal Error' }; // the default implementation of this hook---
37+
---return { message: 'Internal Error' }; // cấu hình mặc định của hook này---
3838
+++return { message: error instanceof Error ? error.message : 'Internal Error' };+++
3939
}
4040
```
4141

4242
```js
4343
// hooks.client.js
4444
export function handleError(+++{ error }+++) {
45-
---return { message: 'Internal Error' }; // the default implementation of this hook---
45+
---return { message: 'Internal Error' }; // cấu hình mặc định của hook này---
4646
+++return { message: error instanceof Error ? error.message : 'Internal Error' };+++
4747
}
4848
```
4949

50-
You could also call your error reporting service in these hooks.
50+
Bạn cũng có thể gọi dịch vụ báo cáo lỗi của mình trong những hook này.
5151

52-
Note that you can return more than an error message if you like. Whatever object shape you return will be available in `$page.error`, the only requirement is a `message` property. You can read more about this (and how to make it type-safe!) in the [error docs](https://kit.svelte.dev/docs/errors).
52+
Lưu ý rằng bạn có thể trả về nhiều hơn một thông báo lỗi nếu bạn muốn. Bất kỳ cấu trúc nào bạn trả về sẽ có sẵn trong `$page.error`, yêu cầu duy nhất là phải có một thuộc tính `message`. Bạn có thể đọc thêm về điều này (và cách thiết lập type-safe!) trong [error docs](https://kit.svelte.dev/docs/errors).
5353

54-
> When handling errors, be careful to not assume it's an `Error` object, anything could be thrown. Also make sure not to expose senstive data by forwarding too much information
54+
> Khi xử lý lỗi, hãy cẩn thận và không nên giả định rằng đó là một đối tượng `Error` vì bất cứ thứ gì cũng có thể throw được. Ngoài ra, hãy đảm bảo bạn không để lộ dữ liệu nhạy cảm bằng cách chuyển tiếp quá nhiều thông tin.
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function handleError() {
2-
return { message: 'Internal Error' }; // the default implementation of this hook
2+
return { message: 'Internal Error' }; // implementation mặc định của hook này
33
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export function handleError() {
2-
return { message: 'Internal Error' }; // the default implementation of this hook
2+
return { message: 'Internal Error' }; // cấu hình mặc định của hook này
33
}

content/tutorial/03-sveltekit/09-errors-and-redirects/xx-custom-error-messages/app-a/src/routes/+error.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
</script>
55

66
{#if !online}
7-
<h1>you're offline</h1>
7+
<h1>bạn đang offline</h1>
88
{:else}
99
<h1>oops!</h1>
10-
<p>Something went wrong</p>
10+
<p>Có gì đó sai sai</p>
1111
{/if}
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<p>Welcome</p>
1+
<p>Chào mừng</p>
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function load() {
22
throw new Error(
3-
'Something went terribly wrong loading the about page'
3+
'Có lỗi nghiêm trọng khi tải trang giới thiệu'
44
);
55
}
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<p>
2-
This is the best about page of all time. Sad
3-
that you'll never see it because of the error.
2+
Đây là trang giới thiệu tốt nhất mọi thời đại. Buồn
3+
thay bạn sẽ không bao giờ nhìn thấy nó vì lỗi.
44
</p>

content/tutorial/03-sveltekit/09-errors-and-redirects/xx-custom-error-messages/app-b/src/routes/+error.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
{#if $page.status === 404}
99
<h1>Not found</h1>
1010
{:else if !online}
11-
<h1>You're offline</h1>
11+
<h1>Bạn đang offline</h1>
1212
{:else}
1313
<h1>Oops!</h1>
1414
<p>{$page.error.message}</p>

0 commit comments

Comments
 (0)