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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Angular 中的 httpClient 調用中生成錯誤?

如何在 Angular 中的 httpClient 調用中生成錯誤?

瀟湘沐 2023-07-06 16:51:36
我的 Angular 應用程序中有以下搜索功能的方法,我想測試錯誤部分。但是,它不會命中錯誤塊。和訂閱有關系嗎?或者我應該使用什么方法或途徑?this.searchGetCall(text).subscribe((res) => {    res = undefined; //test: added to create error    console.log('res', res.constructor());    this.isSearching = false;    this.apiResponse = res;  }, (err) => {    debugger; //cannot hit this block    this.isSearching = false;    console.log('error', err);  });searchGetCall(term: string) {  if (term === '') {    return of([]);  }  return this.httpClient.get('http://www.omdbapi.com/?s=' + term + '&apikey=' + APIKEY, { params: PARAMS.set('search', term) });}
查看完整描述

3 回答

?
郎朗坤

TA貢獻1921條經驗 獲得超9個贊

據我了解,您想在 'res = undefined; 行拋出異常;//測試:添加以創(chuàng)建錯誤'。為此目的使用 try catch。(err) 塊用于處理 http 錯誤。例如,在 C# 中,在操作結果中鍵入以下內容:


   [HttpGet("Get/{id}")]

    public async Task<ActionResult<UserDto>> Get(long id)

    {            

        ServiceResult<IEnumerable<UserBo>> result = await serviceManager.User_Service.FindAsync(filterCriteria);

        if (result.Success)

        {

            userBo = result.Data.FirstOrDefault();

            if (userBo == null)

                return NotFound();

            else

            {

                ServiceResult<bool> resultAutorized = await GetAutorizedUserStatusById(userBo);

                if (!resultAutorized.Success || !resultAutorized.Data)

                    return BadRequest("unauthorized access");

            }


            UserDto userDto = UserBo.ConvertToDto(userBo);


            return userDto;

        }

        else

        {

            return BadRequest(result.Error);

        }

                           .FirstOrDefaultAsync();

    }

注意這一行: return BadRequest("未經授權的訪問");


查看完整回答
反對 回復 2023-07-06
?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

這不像try/catch。訂閱錯誤塊的到達取決于 HTTP 代碼狀態(tài)(403,404,500 等),而不是腳本執(zhí)行錯誤。您的測試只是使腳本崩潰,而不是模擬 HTTP 錯誤代碼。



查看完整回答
反對 回復 2023-07-06
?
慕尼黑5688855

TA貢獻1848條經驗 獲得超2個贊

我假設您的 searchGetCall 是一個網絡請求,并且它返回一個可觀察值。當您的 Web 請求返回 http 狀態(tài)代碼錯誤時,您的錯誤塊將被執(zhí)行。



查看完整回答
反對 回復 2023-07-06
  • 3 回答
  • 0 關注
  • 169 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號