Skip to content
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

refactor: handle also enhanced contentTypes #1251

Merged
merged 9 commits into from
Apr 4, 2024

Conversation

danielpeintner
Copy link
Member

e.g., expected "application/json" should deal with "application/json; charset=utf-8"

fixes #1250

e.g., expected "application/json" should deal with "application/json; charset=utf-8"
@danielpeintner
Copy link
Member Author

danielpeintner commented Mar 11, 2024

Question: is it enough to look into the contentType before ":"

Note1: I assume if we have response: { contentType: "application/json; charset=utf-8" } in a TD we expect to get exactly this encoding ... even though we could run into issues with multiple spaces after ";" etc. Not sure if we want to handle all this..

Note2: I assume we want to have the same check for properties/events as well, right?

Copy link

codecov bot commented Mar 11, 2024

Codecov Report

Attention: Patch coverage is 82.05128% with 7 lines in your changes are missing coverage. Please review.

Project coverage is 77.84%. Comparing base (0b7b108) to head (df20650).

Files Patch % Lines
packages/core/src/consumed-thing.ts 82.05% 7 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1251      +/-   ##
==========================================
+ Coverage   77.81%   77.84%   +0.02%     
==========================================
  Files          84       84              
  Lines       17523    17541      +18     
  Branches     1781     1783       +2     
==========================================
+ Hits        13635    13654      +19     
+ Misses       3853     3852       -1     
  Partials       35       35              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@egekorkan
Copy link
Member

How about checking for string inclusion instead?

@danielpeintner
Copy link
Member Author

How about checking for string inclusion instead?

I think to get it right we might want to use a dedicated library like

I fear that there might be some edge cases we miss otherwise...

@danielpeintner
Copy link
Member Author

danielpeintner commented Mar 12, 2024

Note: I updated accordingly

  • added the same InteractionOutput handling for readProperty also
  • use dedicated library to parse contentType

@egekorkan
Copy link
Member

I like the overall changes. Do we want to apply this to all interaction styles in this PR? In the long run, all should use this library.

Copy link
Member

@JKRhb JKRhb left a comment

Choose a reason for hiding this comment

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

Looks good to me in general :) See one relatively minor comment below:

packages/core/src/consumed-thing.ts Outdated Show resolved Hide resolved
@danielpeintner
Copy link
Member Author

danielpeintner commented Mar 14, 2024

Do we want to apply this to all interaction styles in this PR?

01e0b84 adds the same handling for observeProperty and event

Comment on lines +560 to +565
try {
return this.handleInteractionOutput(content, form, tp);
} catch (e) {
const error = e instanceof Error ? e : new Error(JSON.stringify(e));
throw new Error(`Error while processing property for ${tp.title}. ${error.message}`);
}
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we could maybe also simplify these try-catch-blocks, as the handleInteractionOutput method itself is probably the only place where an error could originate from, right? But this would only be an optimization step.

Copy link
Member Author

Choose a reason for hiding this comment

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

I was thinking about the same. In the first commits the try catch block was inside handleInteractionOutput().

The downside was/is that either the error message is not very clear (not stating for which interaction name & type it originates from) OR we need to pass additional arguments like interaction-type (property, action, event) and interaction-name is used to handleInteractionOutput() purely for logging.
Personally I didn't like that much and hence decided to do the error handling in each interaction even though I know it duplicates code.

Anyhow, I am open to either way...

Copy link
Member

Choose a reason for hiding this comment

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

The downside was/is that either the error message is not very clear (not stating for which interaction name & type it originates from) OR we need to pass additional arguments like interaction-type (property, action, event) and interaction-name is used to handleInteractionOutput() purely for logging.
Personally I didn't like that much and hence decided to do the error handling in each interaction even though I know it duplicates code.

Yeah, I agree, passing these parameters only for logging is definitely not that ideal. So I think we can keep it as is :) Maybe one potential solution for the future could be introducing a common interface for ConsumedThingProperty, ConsumedThingAction, ConsumedThinEvent and then passing the respective interaction affordance to the handler function which could then obtain the necessary information for logging.

@danielpeintner
Copy link
Member Author

Is there anything else I can/should do?

Copy link
Member

@relu91 relu91 left a comment

Choose a reason for hiding this comment

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

In general, I'm a little bit reluctant to add external libraries to the core package unless they are really something needed. However, in this case, I can make an exception given the fact that the package is really just one single file. We can merge!

@relu91 relu91 merged commit 8d2fae9 into eclipse-thingweb:master Apr 4, 2024
12 checks passed
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.

Response not valid if charset is indicated in header
4 participants