Prevent state change when resolved object is not found

First off all I’d like to say that this is my first angular/ionic project so please understand my lack of knowledge :smile:

I’m trying to build a catalog like application, where user can see all items in dashboard view and when he clicks on item then application loads details about selected item and displays it in separate view.

My config function looks like this:

.config(function($stateProvider, $urlRouterProvider, $ionicConfigProvider, $httpProvider) {

    $ionicConfigProvider.scrolling.jsScrolling(true);
    $httpProvider.interceptors.push('httpRequestInterceptor');

    $stateProvider
        .state('login', {
            url: '/login',
            templateUrl: 'view.login/login.html',
            controller: 'LoginCtrl'
        })

        .state('app', {
            url: '/app',
            abstract: true,
            templateUrl: 'view.app/app.html',
            controller: 'AppCtrl'
        })
        
        .state('app.dashboard', {
            url: '/dashboard',
            views: {
                'mainContent': {
                    templateUrl: 'view.dashboard/dashboard.html',
                    controller: 'DashboardCtrl',
                }
            }
        })

        .state('app.details', {
            url: '/details/:itemId',
            views: {
                'mainContent': {
                    templateUrl: 'view.details/details.html',
                    controller: 'DetailsCtrl',
                    resolve: {
                        playlist: function($stateParams, MyDataService) {
                            var playlist = MyDataService.getDetails($stateParams.itemId);
                            if (playlist === null) {
                                console.log("not exists!");
                            } else {
                                return playlist;
                            }
                        }
                    }
                }
            }
        });
    //default
    $urlRouterProvider.otherwise(function($injector) {
        var $state = $injector.get("$state");
        $state.go("login");
    });
});

When I click on item and MyDataService returns item I can see correct details view, but when I change last part of url from lest say 10 to 100 (id that is not correct) I get console.log(“not exists!”) but application still goes to details view (but it is empty).
Can I prevent route in resolve? I’d like to have error stare and whenever MyDataService isn’t returning data I’d like to go to that state.

Thanks for advice!

I’ve fixed my problem by changing resolve a bit:

resolve: {
    playlist: function($stateParams, StockData, $state) {
        var playlist = StockData.getSingle($stateParams.itemId);
        if (playlist === null) {
            console.log("not exists!");
            $state.goto('/error');
        } else {
            return playlist;
        }
    }
}

I’ve added $state and when something is wrong I’m simply calling $state.goto(’/error’);

I’f there is a better way please let me know.