Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle $resource service errors in AngularJS

I am making requests to my API and I am using AngularJS $resource module. It's different from $http so I don't know how to handle my errors.

My service:

var appServices = angular.module('app.services', ['ngResource']); appServices.factory('Category', ['$resource',     function($resource){         return $resource('/apicategoryerr/?format=:format', {}, {             query: {                 method: 'GET',                  params: { format: 'json'},                  isArray: true,              }         });     }]); 

My Controller:

... Category.query(function(data) {                 console.log(data);             }); ... 

I want something like this or .. I don't know a way to handle errors if my API is not working..

Category.query().success(function() {                 console.log('success');             }).error(function() {                 console.log('error');             }); 
like image 535
valkirilov Avatar asked Dec 14 '13 14:12

valkirilov


People also ask

How to handle error in service in Angular?

The basic way to handle errors in Angular is to use Angular's HttpClient service along with RxJS operators throwError and catchError. The HTTP request is made, and it returns the data with a response if anything wrong happens then it returns an error object with an error status code.

How to handle errors in AngularJS?

AngularJS Error HandlingAngularJS provides a service called $exceptionHandler . It handles errors by capturing them and logging them to the console using the $log service, another AngularJS service that wraps up console. log() to make it safe to use if the console object doesn't exist.

What is $resource in AngularJS?

Overview. A factory which creates a resource object that lets you interact with RESTful server-side data sources. The returned resource object has action methods which provide high-level behaviors without the need to interact with the low level $http service. Requires the ngResource module to be installed.


2 Answers

you can pass the error handler as a second parameter toquery.

Category.query(function(data) {}, function() {}); 

EDIT:

to make things a bit clearer, some examples:

var Resource = $resource('/restapi/resource');  Resource.query(function(data) {     // success handler }, function(error) {     // error handler });  Resource.query({     'query': 'thequery' },function(data) {     // success handler }, function(error) {     // error handler });  Resource.query().$promise.then(function(data) {     // success handler }, function(error) {     // error handler });  Resource.query({     'query': 'thequery' }).$promise.then(function(data) {     // success handler }, function(error) {     // error handler }); 
like image 115
marco.eig Avatar answered Oct 31 '22 09:10

marco.eig


You can define a error handler at the creation step of the resource by adding an interceptor object in the description of a method, with a responseError property, linked to your error function.

function resourceErrorHandler(response) { ... }  $resource('/path/:param/', {} ,  {         'get':    {method:'GET',                     interceptor : {responseError : resourceErrorHandler}},         'save':   {method:'POST'},         'query':  {method:'GET', isArray:true,                     interceptor : {responseError : resourceErrorHandler}},         'remove': {method:'DELETE'},         'delete': {method:'DELETE'} }; 

where resourceErrorHandler is a function called on each error on get or query method. For the problem asked, the get method is the only needed. Of course you can apply that to any action.

An other interceptor response exists for $resource to catch a normal response.

 {'get': {method:'GET', interceptor : {response : resourceResponseHandler}}, 

Interceptors are part of the $http module, you can further read about them in their docs.

like image 21
Nicolas Janel Avatar answered Oct 31 '22 09:10

Nicolas Janel