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}`;
}
}
);
}
}

[Angular] Handle HTTP Errors in Angular with HttpErrorResponse interface的更多相关文章

  1. angular源码分析:angular中脏活累活的承担者之$interpolate

    一.首先抛出两个问题 问题一:在angular中我们绑定数据最基本的方式是用两个大括号将$scope的变量包裹起来,那么如果想将大括号换成其他什么符号,比如换成[{与}],可不可以呢,如果可以在哪里配 ...

  2. angular源码分析:angular中入境检察官$sce

    一.ng-bing-html指令问题 需求:我需要将一个变量$scope.x = '<a href="http://www.cnblogs.com/web2-developer/&qu ...

  3. angular的跨域(angular百度下拉提示模拟)和angular选项卡

    1.angular中$http的服务: $http.get(url,{params:{参数}}).success().error(); $http.post(url,{params:{参数}}).su ...

  4. angular源码分析:angular的整个加载流程

    在前面,我们讲了angular的目录结构.JQLite以及依赖注入的实现,在这一期中我们将重点分析angular的整个框架的加载流程. 一.从源代码的编译顺序开始 下面是我们在目录结构哪一期理出的an ...

  5. angular源码分析:angular中jqLite的实现——你可以丢掉jQuery了

    一.从function JQLite(element)函数开始. function JQLite(element) { if (element instanceof JQLite) { //情况1 r ...

  6. angular源码分析:angular的源代码目录结构说明

    一.读源码,是选择"编译合并后"的呢还是"编译前的"呢? 有朋友说,读angular源码,直接看编译后的,多好,不用管模块间的关系,从上往下读就好了.但是在我看 ...

  7. angular源码分析:angular中各种常用函数,比较省代码的各种小技巧

    angular的工具函数 在angular的API文档中,在最前面就是讲的就是angular的工具函数,下面列出来 angular.bind //用户将函数和对象绑定在一起,返回一个新的函数 angu ...

  8. angular源码分析:angular中的依赖注入式如何实现的

    一.准备 angular的源码一份,我这里使用的是v1.4.7.源码的获取,请参考我另一篇博文:angular源码分析:angular源代码的获取与编译环境安装 二.什么是依赖注入 据我所知,依赖注入 ...

  9. angular源码分析:angular源代码的获取与编译环境安装

    一.安装git客户端 1.windows环境推荐使用TortoiseGit. 官网地址:http://tortoisegit.org 下载地址:http://tortoisegit.org/downl ...

随机推荐

  1. JAVA循环迭代中删除或添加集合数据报java.util.ConcurrentModificationException错误

    1.写出下面的输出结果 public class test{ public static void main(String [] args) List<String> list = new ...

  2. 关于C++构造函数一二

    关于构造函数的调用顺序: 1.继承关系 2.从属关系 3.static声明的从属关系 关于拷贝构造函数的声明: classname(const classname & rhs) #includ ...

  3. [C#] 怎样分析stackoverflow等clr错误

    有时候由于无限递归调用等代码错误,w3wp.exe会报错退出.原因是clr.exe出错了. 这样的错误比較难分析,由于C#代码抓不住StackOverflowException等异常. 处理方法是:生 ...

  4. 0x13 链表与邻接表

    这东西我还是有点会玩的啊.. 邻值查找这东西不就是维护个前驱后继嘛.. #include<cstdio> #include<iostream> #include<cstr ...

  5. ubuntu在桌面创建快捷方式

    在/usr/share/applications下列出 *.desktop文件 例如: 首先查看所要创建的快捷方式有么有: ls /usr/share/applications | grep ecli ...

  6. css3子级高度与父级同高,内容垂直居中

    .E-wrap{ overflow: hidden; position: relative; border: 1px solid #ccc; margin: 30px auto 0; width: 5 ...

  7. StatusBarUtils工具类

    import android.app.Activity; import android.app.Dialog; import android.content.Context; import andro ...

  8. iOS 内网内测应用发布

    之前测试时,iOS 开发会把测试版本上传到蒲公英上,可以很方便的获取.后来认为不安全,万一测试版泄露了会有风险,就又回到了解放前,测试跑到开发那里编包.想过把手机越狱安装开发的编的 ipa 包,这样测 ...

  9. Linux常见后缀缩写含义

    ctl: control rc: run control (A run-control file is a file of declarations or commands associated wi ...

  10. 第七章 Python之模块与包

    模块介绍 一个模块就是包含了一组功能的python文件(例如module.py,模块名是module),它从文件级别组织程序,更方便管理,这时我们不仅仅可以把这些文件当作脚本执行,还可以把他们当作模块 ...