Post
KO

공통 에러 interceptor

앵귤러에서 $httpProvider를 이용하여 4개의 시점에 interceptor를 설정할 수 있다.

아래 코드처럼 config 안에서 선언해두면 $http로 요청을 보낸 뒤 Error가 발생되면 해당 에러 메시지를 보여줄 수 있다.

https://docs.angularjs.org/api/ng/service/$http

  • request: interceptors get called with a http config object. The function is free to modify the config object or create a new one. The function needs to return the config object directly, or a promise containing the config or a new config object.
  • requestError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
  • response: interceptors get called with http response object. The function is free to modify the response object or create a new one. The function needs to return the response object directly, or as a promise containing the response or a new responseobject.
  • responseError: interceptor gets called when a previous interceptor threw an error or resolved with a rejection.
$httpProvider.interceptors.push(function($q) { return { 'responseError' : function(response) { alert('Error : ' + response.data.error); } } })
This article is licensed under CC BY 4.0 by the author.