From 02c6f362badcd7aae4cb8fb709adfc38c5da6edc Mon Sep 17 00:00:00 2001 From: Joost Cassee Date: Thu, 5 Nov 2015 10:42:28 +0100 Subject: [PATCH] Improve Resource.$loadPaths error messages --- dist/hypermedia.js | 12 +++++++++--- src/resource.js | 12 +++++++++--- src/resource.spec.js | 2 +- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/dist/hypermedia.js b/dist/hypermedia.js index 58a294a..50d1b91 100644 --- a/dist/hypermedia.js +++ b/dist/hypermedia.js @@ -686,22 +686,28 @@ angular.module('hypermedia') * @return {Promise} a promise that resolves to the resource once all * paths have been loaded */ - $loadPaths: {value: function (paths) { + $loadPaths: {value: function (paths, path_prefix, root_uri) { var self = this; + if (!path_prefix) { + path_prefix = []; + root_uri = self.$uri; + } return self.$load().then(function () { var promises = []; Object.keys(paths).forEach(function (key) { + var full_path = path_prefix.concat(key); var uris = self.$propHref(key); if (!uris) uris = self.$linkHref(key); if (!uris) { - $log.warn('path "' + key + '" not found for resource: ' + self.$uri); + $log.warn('Warning while loading path "' + full_path.join('.') + '" from resource "' + root_uri + '": ' + + 'property or link "' + key + '" not found on resource "' + self.$uri + '"'); return; } uris = angular.isArray(uris) ? uris : [uris]; uris.forEach(function (uri) { var related = (typeof uri === 'string') ? self.$context.get(uri) : uri; - promises.push(related.$loadPaths(paths[key])); + promises.push(related.$loadPaths(paths[key], full_path, root_uri)); }); }); return $q.all(promises); diff --git a/src/resource.js b/src/resource.js index 1ac0a1b..fd6ef45 100644 --- a/src/resource.js +++ b/src/resource.js @@ -242,22 +242,28 @@ angular.module('hypermedia') * @return {Promise} a promise that resolves to the resource once all * paths have been loaded */ - $loadPaths: {value: function (paths) { + $loadPaths: {value: function (paths, path_prefix, root_uri) { var self = this; + if (!path_prefix) { + path_prefix = []; + root_uri = self.$uri; + } return self.$load().then(function () { var promises = []; Object.keys(paths).forEach(function (key) { + var full_path = path_prefix.concat(key); var uris = self.$propHref(key); if (!uris) uris = self.$linkHref(key); if (!uris) { - $log.warn('path "' + key + '" not found for resource: ' + self.$uri); + $log.warn('Warning while loading path "' + full_path.join('.') + '" from resource "' + root_uri + '": ' + + 'property or link "' + key + '" not found on resource "' + self.$uri + '"'); return; } uris = angular.isArray(uris) ? uris : [uris]; uris.forEach(function (uri) { var related = (typeof uri === 'string') ? self.$context.get(uri) : uri; - promises.push(related.$loadPaths(paths[key])); + promises.push(related.$loadPaths(paths[key], full_path, root_uri)); }); }); return $q.all(promises); diff --git a/src/resource.spec.js b/src/resource.spec.js index 53628cf..08bb02d 100644 --- a/src/resource.spec.js +++ b/src/resource.spec.js @@ -266,7 +266,7 @@ describe('Resource', function () { }); resource.$loadPaths({nonexistent: {}}).then(function () { - expect($log.warn).toHaveBeenCalledWith('path "nonexistent" not found for resource: http://example.com'); + expect($log.warn).toHaveBeenCalledWith('Warning while loading path "nonexistent" from resource "http://example.com": property or link "nonexistent" not found on resource "http://example.com"'); done(); }); $rootScope.$digest();