如何在Angular 4上显示服务器端验证消息

时间:2018-03-02 11:14:17

标签: angular asp.net-web-api asp.net-core-webapi

我从我的API返回我的错误对象,我如何在catch中处理它?<​​/ p>

我有一个HttpInterceptor服务,我想全局处理我的验证消息。问题是我无法在角度方面收到我的错误对象。

拦截服务

export class HttpLogInterceptor implements HttpInterceptor {
  sessionData = new Session();

  constructor(private sessionService: SessionService) {

  }

  intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    const started = Date.now();

    const customReq = request.clone({
      headers: request.headers.set('Authorization', 'bearer ' + this.sessionService.getAuthToken())
    });

    return next.handle(customReq).do((event: any) => {
      if (event instanceof HttpResponse) {
        const elapsed = Date.now() - started;
        console.log(`Request for ${request.urlWithParams} took ${elapsed} ms.`);
      }
    }).
      catch(error => {
      return  Observable.of(error);
    });
  }
}

API方法

*[HttpPost]
[Route("api/caller/save")]
public async Task<IActionResult> SaveCaller([FromBody]SaveCallerCommand command)
{
  var result = await mediatr.Send(command);
  return result.ToActionResult();
}
public static IActionResult ToActionResult(this XenResult result)
{
  if (!result.IsFailure)
  return new OkResult();
  if (result.HasException)
  {
    //TODO what error reporting tool will we use to log errors?
    //ErrorSignal.FromCurrentContext().Raise(result.Exception);
  }
  return new BadRequestObjectResult(result.HtmlFormattedFailures);
}*

0 个答案:

没有答案