Skip to content

Commit

Permalink
Merge pull request thinkjs#24 from anjia/patch-11
Browse files Browse the repository at this point in the history
translate
  • Loading branch information
welefen committed Nov 20, 2015
2 parents 3aacda5 + 21eb866 commit c3df4df
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions view/en/doc/2.0/error_handle.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
## 错误处理
## Error Handle

系统在处理用户请求时,会遇到各种各样的错误情况。如:系统内部错误,url 不存在,没有权限,服务不可用等,这些情况下需要给用户显示对应的错误页面。
The system will encounter all kinds of errors when handling user requests. Such as: system internal error, url not exist, permission denied, service unavailable and so on.
In these cases, it needs to show the corresponding error page for users.

### 错误页面
### Error Page

通过 `thinkjs` 命令创建项目时,会自动添加错误处理的逻辑文件以及相应的错误页面。
When using the command `thinkjs` to create project, it will automatically add the error handle logic file and the corresponding error page.

错误逻辑文件路径为 `src/common/controller/error.js`,该文件内容大致如下:
The error logic file is located in `src/common/controller/error.js`, and its content is roughly as follows.

```js
'use strict';
Expand Down Expand Up @@ -67,66 +68,68 @@ export default class extends think.controller.base {
}
```

对应的模版文件路径为 `view/common/error_{Number}.html`
The path of the corresponding error template page is `view/common/error_{Number}.html`.

### 错误类型
### Error Type

系统默认支持的错误类型有 `400``403``404``500` `503`
System default supported error types are `400`, `403`, `404`, `500` and `503`.

#### 400

错误的请求,如:恶意构造一些非法的数据访问、访问的 url 不合法等。
Error request, like maliciously construct some illegal data access, url accessed is illegal and so on.

#### 403

当前访问没有权限。
The current access has no permission.


#### 404

访问的 url 不存在。
The requested url is not found.

#### 500

系统内部出现错误,导致当前请求不可用。
System internal happended error, which leads to the current request is unavailable.


#### 503

服务不可用,需要等到恢复后才能访问。
Service is unavailable until it is recovered.

### Extend Error Type

### 扩展错误类型
You can extend error type in your project depending on the practical requirement. such as adding the specific `600` error, and you can do as the following steps.

项目里可以根据需要扩展错误类型,假如添加一个项目特有的错误 `600`,那么可以通过下面步骤进行:
##### 1. add _600Action

##### 1、添加 _600Action
Add the following codes into `src/common/controller/error.js` file in the appropriate place.

`src/common/controller/error.js` 文件中,合适的位置添加如下的代码:

```js
_600Action(){
return this.displayErrorPage(600);
}
```

##### 2、添加错误页面
##### 2. Add Error Page

添加文件 `view/common/error_600.html`,并在文件里添加显示的错误内容。
Add the file `view/common/error_600.html`, and write the corresponding error information into it.

##### 3、显示错误页面
##### 3. Show Error Page

添加完错误后,需要在对应地方调用显示错误才能让用户看到,可以通过 `think.statusAction` 方法实现。如:
After added the error, you need to call it correspondingly in order to show it for users. It can be achieved by `think.statusAction` method. eg.

```js
export default class extends think.controller.base {
indexAction(){
if(someError){
return think.statusAction(600, this.http); //显示 600 错误,需要将 http 对象传递进去
return think.statusAction(600, this.http); //show 600 error, need to pass http object
}
}
}
```


### 修改错误页面样式

修改错误页面样式,只需要修改对应的模版文件即可,如:修改 `404` 错误则修改模版文件 `view/common/error_404.html`
### Modify Error Page Style

In order to modify the error page style, you just need to modify the corresponding template file. Eg. edit the template file `view/common/error_404.html` to modify `404` error page style.

0 comments on commit c3df4df

Please sign in to comment.