博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface
阅读量:5316 次
发布时间:2019-06-14

本文共 1024 字,大约阅读时间需要 3 分钟。

When communicating with some backend API, data travels over the network using the HTTP protocol. As such, failures may occur, be it on our own device (i.e. the browser) or on the server-side which may not be available or unable to process our request. We need to handle such error responses and give the user a proper feedback.

 

import { HttpErrorResponset } from '@angular/common/http';export class AppComponent {  people;  message;  constructor(private peopleService: PeopleService) {}  fetchPeople() {    this.peopleService      .fetchPeople()      .subscribe(        (data) => {          this.message = null;          this.people = data;        },        (err: HttpErrorResponse) => {          if (err instanceof Error) {            // client-side error            this.message = `An error occured ${err.error.message}`;          } else {            this.message = `Backend returned error code ${err.status}, body was: ${err.message}`;          }        }      );  }}

 

转载于:https://www.cnblogs.com/Answer1215/p/8448860.html

你可能感兴趣的文章
Fine Uploader文件上传组件
查看>>
objective-c overview(二)
查看>>
python查询mangodb
查看>>
驱动的本质
查看>>
Swift的高级分享 - Swift中的逻辑控制器
查看>>
Python数据分析入门案例
查看>>
vue-devtools 获取到 vuex store 和 Vue 实例的?
查看>>
内存地址对齐
查看>>
JavaScript介绍
查看>>
yum 命令跳过特定(指定)软件包升级方法
查看>>
创新课程管理系统数据库设计心得
查看>>
Could not resolve view with name '***' in servlet with name 'dispatcher'
查看>>
pandas 修改指定列中所有内容
查看>>
lua语言入门之Sublime Text设置lua的Build System
查看>>
vue.js基础
查看>>
电脑的自带图标的显示
查看>>
[转载] redis 的两种持久化方式及原理
查看>>
C++ 删除字符串的两种实现方式
查看>>
ORA-01502: 索引'P_ABCD.PK_WEB_BASE'或这类索引的分区处于不可用状态
查看>>
Java抽象类和接口的比较
查看>>