Skip to content

Conversation

jlaur
Copy link
Contributor

@jlaur jlaur commented Aug 23, 2025

The goal of this PR is to prevent NullPointerException rather than catching it. A few IllegalArgumentException were prevented as well.

As principle for this PR, I have not changed behavior.

@jlaur jlaur requested a review from markus7017 as a code owner August 23, 2025 23:17
double power = -1.0;
for (CoIotSensor update : allUpdates) {
CoIotDescrSen d = fixDescription(sensorMap.get(update.id), blkMap);
CoIotDescrSen d = sensorMap.getOrDefault(update.id, new CoIotDescrSen());
Copy link
Contributor

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

Copy link
Contributor Author

@jlaur jlaur Aug 24, 2025

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:

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.

updatesRequested = true;
}
} catch (JsonSyntaxException | IllegalArgumentException | NullPointerException e) {
} catch (Exception e) {
Copy link
Contributor

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

Copy link
Contributor Author

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.

Copy link
Contributor

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?

Copy link
Contributor Author

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;
Copy link
Contributor

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

Copy link
Contributor Author

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:

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here

Copy link
Contributor Author

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) {
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@markus7017 markus7017 changed the title [shelly] Improve error handling [shelly] Improve error handling for Gen1 (COAP/CoIOT) Aug 24, 2025
@jlaur jlaur changed the title [shelly] Improve error handling for Gen1 (COAP/CoIOT) [shelly] Improve error handling for Gen1 (CoAP/CoIoT) Aug 24, 2025
@markus7017
Copy link
Contributor

@jlaur I'll create a version with your changes and do some testing with different Gen1 devices

@jlaur
Copy link
Contributor Author

jlaur commented Aug 26, 2025

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?

@jlaur jlaur force-pushed the shelly-error-handling branch from 386515c to 9da2c4d Compare August 26, 2025 19:25
@jlaur
Copy link
Contributor Author

jlaur commented Sep 7, 2025

@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>
@jlaur jlaur force-pushed the shelly-error-handling branch from 9da2c4d to ea163e0 Compare September 9, 2025 20:25
@lsiepel lsiepel requested a review from markus7017 September 16, 2025 19:19
@jlaur
Copy link
Contributor Author

jlaur commented Sep 18, 2025

@markus7017?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants