我正在用角7,净核对应用程序进行编程,我正在使用ngx-translate包在客户端https://github.com/ngx-translate/core中进行翻译。问题是,我在控制器中的捕获中有一个动态代码,用于处理并发错误和唯一键错误,然后,我处理了特定的错误,并举杯了。问题是:如何使用ngx-translate转换此错误?
ProductController.cs
在第一个捕获中,我们具有唯一键错误,在第二个捕获中,我们具有并发错误。
[HttpPut("{id}")] // /api/product/4
public async Task<IActionResult> Put([FromBody]ProductFormResource productFormResource, int id)
{
try
{
if (!ModelState.IsValid)
return BadRequest(ModelState);
if (await _productCore.Update(id, productFormResource))
return Ok();
}
catch (DbUpdateException ex) when (ex.InnerException is SqlException sqlEx && (sqlEx.Number == 2601 || sqlEx.Number == 2627))
{
ConfigureMessageExceptionUniqueKey(productFormResource, ex, out string messageUK);
return BadRequest(messageUK);
}
catch (DbUpdateConcurrencyException ex)
{
var databaseEntry = ex.Entries.Single().GetDatabaseValues();
if (databaseEntry == null)
ModelState.AddModelError(string.Empty, "Unable to save changes. The register was deleted by another user.");
else
ModelState.AddModelError(string.Empty, string.Concat("The record you attempted to edit ", "was modified by another user after you got the original value. The ", "edit operation was canceled and the current values in the database ", "have been displayed."));
return BadRequest(ModelState);
}
catch (Exception ex)
{
_logger.LogError($"Fail saving data. (Updating) {ex.Message}");
}
return BadRequest($"Fail saving data. (Updating)");
}
private void ConfigureMessageExceptionUniqueKey(ProductFormResource product, DbUpdateException ex, out string messageUK)
{
string[] keys = { "Description", "Code" };
messageUK = "";
if (ex.InnerException != null)
{
if (ex.InnerException.Message.Contains(keys[0]))
messageUK = $"There is already {product.Description}. Cannot save a value duplicate in Description.";
if (ex.InnerException.Message.Contains(keys[1]))
messageUK = $"There is already {product.Code}. Cannot save a value duplicate in Code.";
}
}
ProductService
我打电话给服务器并处理任何错误
put(id, productPut): Observable<any> {
return this.http.put(`${this.productEndPoint}/${id}`, productPut)
.pipe(tap(m => console.log('put')), catchError(this.handleError));
}
handleError(error) {
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// client-side error
errorMessage = `Error: ${error.error.message}`;
} else {
// server-side error
console.log(error)
errorMessage = `${error.error}\nError Code: ${error.status}`;
}
return throwError(errorMessage);
}
product-form.component.ts
我在烤面包上显示了错误
onSubmit() {
var result$ = (this.product.id) ? this.productService.put(this.product.id, this.product) :
this.productService.post(this.product);
result$.subscribe(mt => {
this.toastrService.success('Data saved successfully!', 'Good!', {closeButton: true});
this.router.navigate([`/inventory-product-list/`]);
},
err => { this.toastrService.error(err, 'An error has ocurred!', { closeButton: true }); }
);
}
关于如何翻译这些消息的一些想法?感谢您的评论,