@ -29,11 +29,11 @@ Error Message is an instance of the `RemoteServiceErrorResponse` class. The simp
}
````
There are **optional fields** those can be filled based upon the exception that has occured.
当异常发生时,会自动填充到这些**可选字段**.
##### Error Code
##### 错误代码
Error **code** is an optional and unique string value for the exception. Thrown `Exception` should implement the `IHasErrorCode` interface to fill this field. Example JSON value:
@ -44,11 +44,11 @@ Error **code** is an optional and unique string value for the exception. Thrown
}
````
Error code can also be used to localize the exception and customize the HTTP status code (see the related sections below).
错误 **Code** 同样可用于异常的本地化及自定义HTTP状态代码(请参阅下面的相关部分).
##### Error Details
##### 错误详细信息
Error **details** in an optional field of the JSON error message. Thrown `Exception` should implement the `IHasErrorDetails` interface to fill this field. Example JSON value:
@ -81,15 +81,15 @@ Error **details** in an optional field of the JSON error message. Thrown `Except
}
````
`AbpValidationException` implements the `IHasValidationErrors` interface and it is automatically thrown by the framework when a request input is not valid. So, usually you don't need to deal with validation errors unless you have higly customised validation logic.
Exceptions are logged with the `Error` level by default. The Log level can be determined by the exception if it implements the `IHasLogLevel` interface. Example:
`BusinessException`implements the `IBusinessException` interface in addition to the `IHasErrorCode`, `IHasErrorDetails` and `IHasLogLevel` interfaces. The default log level is `Warning`.
One problem with throwing exceptions is how to localize error messages while sending it to the client. ABP offers two models and their variants.
这里有个问题,就是如何在发送错误消息到客户端时,对错误消息进行本地化.ABP提供了2个模型.
#### User Friendly Exception
#### 用户友好异常
If an exception implements the `IUserFriendlyException` interface, then ABP does not change it's `Message` and `Details` properties and directly send it to the client.
@ -161,13 +161,13 @@ throw new UserFriendlyException(
);
````
In this way, there is **no need for localization** at all. If you want to localize the message, you can inject and use the standard **string localizer** (see the [localization document](Localization.md)). Example:
throw new UserFriendlyException(_stringLocalizer["UserNameShouldBeUniqueMessage"]);
````
Then define it in the **localization resource** for each language. Example:
再在本地化资源的语言中添加对应的定义.例如:
````json
{
@ -178,30 +178,30 @@ Then define it in the **localization resource** for each language. Example:
}
````
String localizer already supports **parameterized messages**. For example:
**string localizer** 是支持格式化参数.例如
````C#
throw new UserFriendlyException(_stringLocalizer["UserNameShouldBeUniqueMessage", "john"]);
````
Then the localization text can be:
其本地化文本如下:
````json
"UserNameShouldBeUniqueMessage": "Username should be unique! '{0}' is already taken!"
````
* The `IUserFriendlyException` interface is derived from the `IBusinessException` and the `UserFriendlyException` class is derived from the `BusinessException` class.
Then any of the exceptions with `Volo.Qa` namespace will be localized using their given localization resource. The localization resource should always have an entry with the error code key. Example:
@ -221,18 +221,18 @@ Then any of the exceptions with `Volo.Qa` namespace will be localized using thei
}
````
Then a business exception can be thrown with the error code:
最后就可以抛出一个包含错误代码的业务异常了:
````C#
throw new BusinessException(QaDomainErrorCodes.CanNotVoteYourOwnAnswer);
````
* Throwing any exception implementing the `IHasErrorCode` interface behaves the same. So, the error code localization approach is not unique to the `BusinessException` class.
* Defining localized string is not required for an error message. If it's not defined, ABP sends the default error message to the client. It does not use the `Message` property of the exception! if you want that, use the `UserFriendlyException` (or use an exception type that implements the `IUserFriendlyException` interface).
The `IHttpExceptionStatusCodeFinder` is used to automatically determine the HTTP status code. The default implementation is the `DefaultHttpExceptionStatusCodeFinder` class. It can be replaced or extended as needed.
Some exception types are automatically thrown by the framework:
框架会自动抛出以下异常类型:
- `AbpAuthorizationException` is thrown if the current user has no permission to perform the requested operation. See authorization document (TODO: link) for more.
- `AbpValidationException` is thrown if the input of the current request is not valid. See validation document (TODO: link) for more.
- `EntityNotFoundException` is thrown if the requested entity is not available. This is mostly thrown by [repositories](Repositories.md).