第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Angular:無法通過 HttpResponse 返回自定義錯(cuò)誤消息

Angular:無法通過 HttpResponse 返回自定義錯(cuò)誤消息

C#
喵喵時(shí)光機(jī) 2021-10-31 20:05:40
我一直在谷歌搜索解決方法并看到我們?cè)诳刂破鞑僮髦凶鳛锳ctionResult/JsonResult或使用HttpRequest如下方法返回錯(cuò)誤文本時(shí)的示例HttpContext.Current.Response.Status = "error text";對(duì)于我的后臺(tái)應(yīng)用程序,我使用ASP.NET Core 2.1.1和.Status屬性在缺少HttpResponse類。此外,我找不到任何可能包含我的自定義錯(cuò)誤消息的屬性。我使用中間件類,它獲取異常描述并將其作為 JSONStartup.csapp.UseMiddleware<ExceptionHandleMiddleware>();班級(jí)本身public class ExceptionHandleMiddleware{    private readonly RequestDelegate next;    public ExceptionHandleMiddleware(RequestDelegate next)    {        this.next = next ?? throw new ArgumentNullException(nameof(next));    }    public async Task Invoke(HttpContext context)    {        try        {            await next(context);        }        catch (Exception ex)        {            context.Response.Clear();            context.Response.Headers.Add("Access-Control-Allow-Origin", "*");            context.Response.ContentType = "application/json";            context.Response.StatusCode = StatusCodes.Status500InternalServerError;            await context.Response.WriteAsync(JsonConvert.SerializeObject(new { error = $"{ex.GetType().FullName}: '{ex.Message}'" }));        }    }}看看行context.Response.StatusCode = StatusCodes.Status500InternalServerError;這是必需的,因?yàn)樵谖业腁ngular 6 應(yīng)用程序中,我使用HttpInterceptor,并且為了捕獲錯(cuò)誤,您應(yīng)該返回一個(gè) HTTP 錯(cuò)誤(否則.catch(...)在 Angular 攔截器中不會(huì)觸發(fā)該塊)。這是我的 Angular 應(yīng)用程序中的一點(diǎn)@Injectable()export class ErrorHandlerInterceptor implements HttpInterceptor {    constructor(        private notify: NgNotify,    ) { }    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {        return next            .handle(req)            .catch(this.handleError)    }    ...盡管context.Response.WriteAsync(...)bit確實(shí)返回了異常文本,但我無法.catch(...)在攔截器的塊中提取它。所以,我似乎無法從error: Response.也許,有人知道將錯(cuò)誤傳遞給 Angular 客戶端并在那里獲取它的更好方法?
查看完整描述

2 回答

?
慕桂英3389331

TA貢獻(xiàn)2036條經(jīng)驗(yàn) 獲得超8個(gè)贊

我的疏忽。


如果您查看返回的 JSON


  ....


  "message":"Http failure response for https://localhost:44305/api/Entry/GetNext?id=11962: 500 OK",

  "error":{  


  }

}

error似乎是一個(gè)空對(duì)象


代替事實(shí)上error是Blob,我們應(yīng)該以下面的方式閱讀


intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

    return next

        .handle(req)

        .catch(this.handleError)

}


public handleError = (error: Response) => {


    let reader = new FileReader();


    let ngNotify = this._ngNotify;


    reader.onload = function () {


        var result = JSON.parse(this.result);


        ngNotify.nofity('Error', result.error);

    };


    reader.readAsText(error['error']);


    return Observable.throw(error)

}

就是這樣。


查看完整回答
反對(duì) 回復(fù) 2021-10-31
?
茅侃侃

TA貢獻(xiàn)1842條經(jīng)驗(yàn) 獲得超21個(gè)贊

我最終通過實(shí)現(xiàn)中間件來攔截HttpResponse、從 blob 中提取錯(cuò)誤并在 json 中返回消息來解決這個(gè)問題。感謝JaapMosselman 的貢獻(xiàn)


查看完整回答
反對(duì) 回復(fù) 2021-10-31
  • 2 回答
  • 0 關(guān)注
  • 567 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)