mirror of
https://github.com/yiisoft/yii2.git
synced 2025-12-01 23:51:12 +08:00
docs/guide-ja/runtime-handling-errors.md - completed [ci skip]
This commit is contained in:
@@ -118,10 +118,9 @@ class SiteController extends Controller
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The above code defines the `error` action using the [[yii\web\ErrorAction]] class which renders an error
|
上記のコードは [[yii\web\ErrorAction]] クラスを使って `error` アクションを定義しています。[[yii\web\ErrorAction]] クラスは `error` という名前のビューを使ってエラーをレンダリングします。
|
||||||
using a view named `error`.
|
|
||||||
|
|
||||||
Besides using [[yii\web\ErrorAction]], you may also define the `error` action using an action method like the following,
|
[[yii\web\ErrorAction]] を使う以外に、次のようにアクションメソッドを使って `error` アクションを定義することも出来ます。
|
||||||
|
|
||||||
```php
|
```php
|
||||||
public function actionError()
|
public function actionError()
|
||||||
@@ -133,26 +132,26 @@ public function actionError()
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You should now create a view file located at `views/site/error.php`. In this view file, you can access
|
次に `views/site/error.php` に配置されるビューファイルを作成しなければなりません。エラーアクションが [[yii\web\ErrorAction]]
|
||||||
the following variables if the error action is defined as [[yii\web\ErrorAction]]:
|
として定義されている場合は、このビューファイルの中で次の変数にアクセスすることが出来ます。
|
||||||
|
|
||||||
* `name`: the name of the error;
|
* `name`: エラーの名前。
|
||||||
* `message`: the error message;
|
* `message`: エラーメッセージ。
|
||||||
* `exception`: the exception object through which you can more useful information, such as HTTP status code,
|
* `exception`: 例外オブジェクト。これを通じて、更に有用な情報、例えば、HTTP ステータスコード、
|
||||||
error code, error call stack, etc.
|
エラーコード、エラーコールスタックなどにアクセス出来ます。
|
||||||
|
|
||||||
> Info: If you are using the [basic application template](start-installation.md) or the [advanced application template](tutorial-advanced-app.md),
|
> Info|情報: あなたが [ベーシックアプリケーションテンプレート](start-installation.md) または
|
||||||
the error action and the error view are already defined for you.
|
[アドバンストアプリケーションテンプレート](tutorial-advanced-app.md) を使っている場合は、
|
||||||
|
エラーアクションとエラービューは、既にあなたのために定義されています。
|
||||||
|
|
||||||
|
|
||||||
### Customizing Error Response Format <a name="error-format"></a>
|
### エラーのレスポンス形式をカスタマイズする <a name="error-format"></a>
|
||||||
|
|
||||||
The error handler displays errors according to the format setting of the [response](runtime-responses.md).
|
エラーハンドラは、[レスポンス](runtime-responses.md) の形式の設定に従ってエラーを表示します。
|
||||||
If the the [[yii\web\Response::format|response format]] is `html`, it will use the error or exception view
|
[[yii\web\Response::format|レスポンス形式]] が `html` である場合は、直前の項で説明したように、
|
||||||
to display errors, as described in the last subsection. For other response formats, the error handler will
|
エラービューまたは例外ビューを使ってエラーを表示します。その他のレスポンス形式の場合は、
|
||||||
assign the array representation of the exception to the [[yii\web\Response::data]] property which will then
|
エラーハンドラは例外の配列表現を [[yii\web\Response::data]] プロパティに代入し、次に `data` プロパティがレスポンス形式に応じて様々な形式に変換されます。
|
||||||
be converted to different formats accordingly. For example, if the response format is `json`, you may see
|
例えば、レスポンス形式が `json` である場合は、次のようなレスポンスになります。
|
||||||
the following response:
|
|
||||||
|
|
||||||
```
|
```
|
||||||
HTTP/1.1 404 Not Found
|
HTTP/1.1 404 Not Found
|
||||||
@@ -163,14 +162,14 @@ Content-Type: application/json; charset=UTF-8
|
|||||||
|
|
||||||
{
|
{
|
||||||
"name": "Not Found Exception",
|
"name": "Not Found Exception",
|
||||||
"message": "The requested resource was not found.",
|
"message": "リクエストされたリソースは見つかりませんでした。",
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"status": 404
|
"status": 404
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
You may customize the error response format by responding to the `beforeSend` event of the `response` component
|
エラーのレスポンス形式をカスタマイズするために、アプリケーションのコンフィギュレーションの中で、
|
||||||
in the application configuration:
|
`response` コンポーネントの `beforeSend` イベントに反応するハンドラを構成することが出来ます。
|
||||||
|
|
||||||
```php
|
```php
|
||||||
return [
|
return [
|
||||||
@@ -193,7 +192,7 @@ return [
|
|||||||
];
|
];
|
||||||
```
|
```
|
||||||
|
|
||||||
The above code will reformat the error response like the following:
|
上記のコードは、エラーのレスポンスを以下のようにフォーマットし直すものです。
|
||||||
|
|
||||||
```
|
```
|
||||||
HTTP/1.1 200 OK
|
HTTP/1.1 200 OK
|
||||||
@@ -206,7 +205,7 @@ Content-Type: application/json; charset=UTF-8
|
|||||||
"success": false,
|
"success": false,
|
||||||
"data": {
|
"data": {
|
||||||
"name": "Not Found Exception",
|
"name": "Not Found Exception",
|
||||||
"message": "The requested resource was not found.",
|
"message": "リクエストされたリソースは見つかりませんでした。",
|
||||||
"code": 0,
|
"code": 0,
|
||||||
"status": 404
|
"status": 404
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user