Skip to content

Commit

Permalink
[#108] Improve error message when concept not found
Browse files Browse the repository at this point in the history
Add a new error route under the vocabulary route, so that an error condition do not cause the user to get kicked out of the currently selected vocabulary.

And omprove the error message shown when a user comes across a concept which does not exists.
  • Loading branch information
danmichaelo committed Jun 6, 2019
1 parent f65134b commit c2b9388
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/app/pages/error.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

<div class="container">
<div class="vocabulary-info my-3">
<div class="alert alert-danger">
{{ vm.message }}
</div>
Expand Down
11 changes: 6 additions & 5 deletions src/app/pages/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ function ErrorController($state, $stateParams, gettext, gettextCatalog) {
/*jshint validthis: true */
var vm = this;

if ($state.get('error').error) {
vm.message = $state.get('error').error.message;
var msg = gettext('An unknown error occured');
var translated = gettextCatalog.getString(msg);

if ($state.get('error').message) {
vm.message = translated + ': ' + $state.get('error').message;
} else {
var msg = gettext('An unknown error occured.');
var translated = gettextCatalog.getString(msg);
vm.message = translated;
vm.message = translated + '.';
}
}
65 changes: 35 additions & 30 deletions src/app/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ function configure($stateProvider, $urlRouterProvider) {
'main': 'appMain',
},
})
.state('subject.error', {
url: '/error',
views: {
'searchBox': 'appSearchBox',
'catalogue': 'appError',
},
})
.state('subject.search', {
url: '/search?term&id&uri&broad&library',
views: {
Expand All @@ -61,9 +68,14 @@ function configure($stateProvider, $urlRouterProvider) {
'info': 'appVocabularyInfo',
},
resolve: {
subject: /* @ngInject */ function (AuthorityService, $stateParams, $q, $state) {
subject: /* @ngInject */ function (AuthorityService, $stateParams, $q, $state, $timeout) {
var deferred = $q.defer();

if (!$stateParams.term && !$stateParams.id && !$stateParams.uri) {
return
}

if ($stateParams.vocab == 'ubo') {
var deferred = $q.defer();

AuthorityService.lookupUboClassification($stateParams.term)
.then(
Expand All @@ -77,29 +89,24 @@ function configure($stateProvider, $urlRouterProvider) {
uri: null,
});
return deferred.reject('Redirecting');
},
err => {
}
)
.catch(
err => {
return deferred.reject('Concept Not Found');
}
)
.catch(err => deferred.reject(`The classification code ${stateParams.term} was not found in any of the supported classification schemes.`));

} else {

return deferred.promise;
}
AuthorityService.lookup($stateParams)
.then(result => deferred.resolve(result))
.catch(err => {
if ($stateParams.term) {
deferred.reject(`The search term "${$stateParams.term}" was not found in this vocabulary.`);
} else {
deferred.reject('The search term was not found in this vocabulary.');
}
})

if ($stateParams.uri) {
return AuthorityService.getByUri($stateParams.uri);
} else if ($stateParams.term) {
return AuthorityService.getByTerm($stateParams.term, $stateParams.vocab);
} else if ($stateParams.id) {
return AuthorityService.getById($stateParams.id, $stateParams.vocab);
} else {
// Exception
}
return deferred.promise;
}
}
});
Expand All @@ -125,22 +132,19 @@ function run($rootScope, $state, $transitions, AuthorityService, TitleService) {

$transitions.onError({}, function (transition) {
var error = transition.error();
console.log(error);
var targetStateParams = transition.targetState().params();
log.error('Error while transitioning to a new state: ', error);
if (angular.isObject(error) && angular.isString(error.code)) {
switch (error.code) {
case 'NOT_AUTHENTICATED':
// go to the login page
$state.go('login');
break;
return;
default:
// set the error object on the error state and go there
$state.get('error').error = error;
// $state.go('error');
$state.get('error').message = error.message;
}
return;
}
if (angular.isObject(error)) {
} else if (angular.isObject(error)) {
console.log('Trans error', error.type, error.message)
if (error.type == 5) {
// No transition was necessary, this is fine.
Expand All @@ -150,12 +154,13 @@ function run($rootScope, $state, $transitions, AuthorityService, TitleService) {
// The transition has been superseded by a different transition, also fine.
return;
}
if (error.detail == 'Concept Not Found') {
// The transition has been superseded by a different transition, also fine.
return;
}

// unexpected error
$state.get('error').message = error.detail;
}
if (targetStateParams.vocab) {
$state.go('subject.error', {vocab: targetStateParams.vocab});
} else {
$state.go('error');
}
});
Expand Down
18 changes: 18 additions & 0 deletions src/app/services/authority.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ function AuthorityService($http, $stateParams, $filter, $q, $rootScope, gettext,
getByUri: getByUri,
getByTerm: getByTerm,
getVocabulary: getVocabulary,
lookup: lookup,
lookupUboClassification: lookupUboClassification,
exists: exists,
onSubject: onSubject,
Expand Down Expand Up @@ -390,6 +391,23 @@ function AuthorityService($http, $stateParams, $filter, $q, $rootScope, gettext,
return deferred.promise;
}

async function lookup(params) {
try {
if (params.uri) {
return await getByUri(params.uri);
}
if (params.term) {
return await getByTerm(params.term, params.vocab);
}
if (params.id) {
return await getById(params.id, params.vocab);
}
} catch (err) {
throw new Error('Concept not found');
}
throw new Error('Unknown search parameters');
}

async function lookupUboClassification(term) {
var deferred = $q.defer();

Expand Down

0 comments on commit c2b9388

Please sign in to comment.