-
-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[shelly] Improve error handling for Gen1 (CoAP/CoIoT) #19229
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
double power = -1.0; | ||
for (CoIotSensor update : allUpdates) { | ||
CoIotDescrSen d = fixDescription(sensorMap.get(update.id), blkMap); | ||
CoIotDescrSen d = sensorMap.getOrDefault(update.id, new CoIotDescrSen()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why did you removed the call to fixDescription(), this is important
I'm not fine with allocation of an empty CoIotDescrSen() - if the process fails, this is an error
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the method previously called:
Lines 373 to 375 in c417fe3
public CoIotDescrSen fixDescription(@Nullable CoIotDescrSen sen, Map<String, CoIotDescrBlk> blkMap) { | |
return sen != null ? sen : new CoIotDescrSen(); | |
} |
So what you are suggesting is to change that behavior. As principle for this PR I did not want to change behavior, because I don't know the logic well enough and I don't have Gen 1 devices.
...inding.shelly/src/main/java/org/openhab/binding/shelly/internal/api1/Shelly1CoapHandler.java
Outdated
Show resolved
Hide resolved
updatesRequested = true; | ||
} | ||
} catch (JsonSyntaxException | IllegalArgumentException | NullPointerException e) { | ||
} catch (Exception e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are discussing to use IllegalArgumentException or RuntimeException all the time and now you catch all Exception generically. Also I thing explicitly point our that it could result in a JsonSyntaxException has a better quality. I would prefer JsonSyntaxException | RuntimeException
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed several calls to org.eclipse.californium.core.coap
, i.e. a third party library outside of our control. If you are sure none of those exceptions are thrown by that library, we can remove the catch
entirely. I checked and ensured that the binding doesn't throw any of those anymore.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you parsed a malformed JSON to gson.fromJson() this will cause JsonSyntaxException.
What do you think about JsonSyntaxException | RuntimeException?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where do you see gson.fromJson()
within this try/catch block?
if (!valid) { | ||
logger.debug("{}: WARNING: Incompatible device description detected for CoIoT version {}!", thingName, | ||
coiot.getVersion()); | ||
return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm, that does not provide the call stack, which makes it easy to fix things
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as before. I'm not sure what you are suggesting? This message is only logged at this specific place, so at least you know where it's coming from.
addSensor
logs before returning false:
Lines 391 to 392 in c417fe3
logger.debug("{}: Invalid format for sensor definition detected, id={}", thingName, sen.id); | |
return false; |
// description is buggy | ||
logger.debug("{}: Unable to decode sensor definition -> skip", thingName, e); | ||
} | ||
CoIotDescrSen fixed = coiot.fixDescription(sen, blkMap); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as what?
logger.trace("{}: Sensor value[{}]: id={}, Value={} ({}, Type={}, Range={}, Link={}: {})", thingName, | ||
i, s.id, getString(s.valueStr).isEmpty() ? s.value : s.valueStr, sen.desc, sen.type, sen.range, | ||
sen.links, element.desc); | ||
for (CoIotSensor s : sensorUpdates) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you tested the logic changes with multiple Gen1 devices?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately I don't have any Gen 1 devices, so I have to rely on testing when integrated in snapshot builds and the DEV build, is that okay? There should be no logic changes here though, I did this refactoring very carefully step by step. If you see any, please let me know.
@jlaur I'll create a version with your changes and do some testing with different Gen1 devices |
Thank you,. 👍 In the meantime, can we discuss the open comments? |
386515c
to
9da2c4d
Compare
@markus7017 - did you see my comments? |
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
9da2c4d
to
ea163e0
Compare
The goal of this PR is to prevent
NullPointerException
rather than catching it. A fewIllegalArgumentException
were prevented as well.As principle for this PR, I have not changed behavior.