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

View file

@ -62,14 +62,22 @@ export const wotStoreModule = {
thingAvailable: state => thingName => { thingAvailable: state => thingName => {
return thingName in state.thingDescriptions; 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 // Find the URL for a particular operation
let td = state.thingDescriptions[thing]; let td = state.thingDescriptions[thing];
if (!td) return null; if (!td) return null;
let affordances = td[affordanceType]; let affordances = td[affordanceType];
let href = findFormHref(affordances[affordance], op); 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 we've found an href, prepend the `base` URL if appropriate
if (href.startsWith("http")) return href; if (href.startsWith("http")) return href;
if ("base" in td) { if ("base" in td) {
@ -80,13 +88,29 @@ export const wotStoreModule = {
} }
return href; return href;
}, },
thingPropertyUrl: (_state, getters) => (thing, property, op) => { thingPropertyUrl: (_state, getters) => (
thing,
property,
op,
allowUndefined
) => {
// Find the URL for a particular property // 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 // Find the URL for a particular action
return getters.thingFormUrl(thing, "actions", action, op); return getters.thingFormUrl(thing, "actions", action, op, allowUndefined);
} }
} }
}; };