Add error checking to mixin thing functions

Now we'll get an error if undefined URLs are about to be fetched.
The error will contain thing and affordance, for debugging.
This commit is contained in:
Richard Bowman 2023-12-01 02:43:17 +00:00
parent ee641f5cd2
commit 0cd8ecd2d5
2 changed files with 39 additions and 11 deletions

View file

@ -45,7 +45,8 @@ Vue.mixin({
let url = this.$store.getters["wot/thingPropertyUrl"](
thing,
property,
"readproperty"
"readproperty",
false
);
try {
let response = await axios.get(url);
@ -59,7 +60,8 @@ Vue.mixin({
let url = this.$store.getters["wot/thingPropertyUrl"](
thing,
property,
"writeproperty"
"writeproperty",
false
);
try {
await axios.put(url, value);
@ -68,11 +70,13 @@ Vue.mixin({
}
},
thingActionUrl(thing, action) {
return this.$store.getters["wot/thingActionUrl"](
let url = this.$store.getters["wot/thingActionUrl"](
thing,
action,
"invokeaction"
"invokeaction",
false
);
return url;
},
modalConfirm: function(modalText) {
var context = this;

View file

@ -62,14 +62,22 @@ export const wotStoreModule = {
thingAvailable: state => thingName => {
return thingName in state.thingDescriptions;
},
thingFormUrl: state => (thing, affordanceType, affordance, op) => {
thingFormUrl: state => (
thing,
affordanceType,
affordance,
op,
allowUndefined = true
) => {
// Find the URL for a particular operation
let td = state.thingDescriptions[thing];
if (!td) return null;
let affordances = td[affordanceType];
let href = findFormHref(affordances[affordance], op);
if (href == undefined) return undefined;
if (href == undefined) {
if (allowUndefined) return undefined;
throw `Could not find form for ${affordanceType} ${thing}/${affordance} with op ${op}`;
}
// If we've found an href, prepend the `base` URL if appropriate
if (href.startsWith("http")) return href;
if ("base" in td) {
@ -80,13 +88,29 @@ export const wotStoreModule = {
}
return href;
},
thingPropertyUrl: (_state, getters) => (thing, property, op) => {
thingPropertyUrl: (_state, getters) => (
thing,
property,
op,
allowUndefined
) => {
// Find the URL for a particular property
return getters.thingFormUrl(thing, "properties", property, op);
return getters.thingFormUrl(
thing,
"properties",
property,
op,
allowUndefined
);
},
thingActionUrl: (_state, getters) => (thing, action, op) => {
thingActionUrl: (_state, getters) => (
thing,
action,
op,
allowUndefined
) => {
// Find the URL for a particular action
return getters.thingFormUrl(thing, "actions", action, op);
return getters.thingFormUrl(thing, "actions", action, op, allowUndefined);
}
}
};