@@ -21,6 +21,8 @@ import Servient from "./servient";
2121import Helpers from "./helpers" ;
2222
2323import { ProtocolClient } from "./protocol-interfaces" ;
24+ import { Content } from "./content" ;
25+ import ContentType from "content-type" ;
2426
2527import ContentManager from "./content-serdes" ;
2628
@@ -555,7 +557,33 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
555557 form = this . handleUriVariables ( tp , form , options ) ;
556558
557559 const content = await client . readResource ( form ) ;
558- return new InteractionOutput ( content , form , tp ) ;
560+ try {
561+ return this . handleInteractionOutput ( content , form , tp ) ;
562+ } catch ( e ) {
563+ const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
564+ throw new Error ( `Error while processing property for ${ tp . title } . ${ error . message } ` ) ;
565+ }
566+ }
567+
568+ private handleInteractionOutput (
569+ content : Content ,
570+ form : TD . Form ,
571+ outputDataSchema : WoT . DataSchema | undefined
572+ ) : InteractionOutput {
573+ // infer media type from form if not in response metadata
574+ content . type ??= form . contentType ?? "application/json" ;
575+
576+ // check if returned media type is the same as expected media type (from TD)
577+ if ( form . response != null ) {
578+ const parsedMediaTypeContent = ContentType . parse ( content . type ) ;
579+ const parsedMediaTypeForm = ContentType . parse ( form . response . contentType ) ;
580+ if ( parsedMediaTypeContent . type !== parsedMediaTypeForm . type ) {
581+ throw new Error (
582+ `Unexpected type '${ content . type } ' in response. Should be '${ form . response . contentType } '`
583+ ) ;
584+ }
585+ }
586+ return new InteractionOutput ( content , form , outputDataSchema ) ;
559587 }
560588
561589 async _readProperties ( propertyNames : string [ ] ) : Promise < WoT . PropertyReadMap > {
@@ -674,19 +702,11 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
674702 form = this . handleUriVariables ( ta , form , options ) ;
675703
676704 const content = await client . invokeResource ( form , input ) ;
677- // infer media type from form if not in response metadata
678- if ( ! content . type ) content . type = form . contentType ?? "application/json" ;
679-
680- // check if returned media type is the same as expected media type (from TD)
681- if ( form . response != null ) {
682- if ( content . type !== form . response . contentType ) {
683- throw new Error ( `Unexpected type in response` ) ;
684- }
685- }
686705 try {
687- return new InteractionOutput ( content , form , ta . output ) ;
688- } catch {
689- throw new Error ( `Received invalid content from Thing` ) ;
706+ return this . handleInteractionOutput ( content , form , ta . output ) ;
707+ } catch ( e ) {
708+ const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
709+ throw new Error ( `Error while processing action for ${ ta . title } . ${ error . message } ` ) ;
690710 }
691711 }
692712
@@ -725,12 +745,11 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
725745 formWithoutURITemplates ,
726746 // next
727747 ( content ) => {
728- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- tsc get confused when nullables are to listeners lambdas
729- if ( ! content . type ) content . type = form ! . contentType ?? "application/json" ;
730748 try {
731- listener ( new InteractionOutput ( content , form , tp ) ) ;
749+ listener ( this . handleInteractionOutput ( content , form , tp ) ) ;
732750 } catch ( e ) {
733- warn ( `Error while processing observe event for ${ tp . title } ` ) ;
751+ const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
752+ warn ( `Error while processing observe property for ${ tp . title } . ${ error . message } ` ) ;
734753 warn ( e ) ;
735754 }
736755 } ,
@@ -782,12 +801,11 @@ export default class ConsumedThing extends TD.Thing implements IConsumedThing {
782801 await client . subscribeResource (
783802 formWithoutURITemplates ,
784803 ( content ) => {
785- // eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- tsc get confused when nullables are to listeners lambdas
786- if ( ! content . type ) content . type = form ! . contentType ?? "application/json" ;
787804 try {
788- listener ( new InteractionOutput ( content , form , te . data ) ) ;
805+ listener ( this . handleInteractionOutput ( content , form , te . data ) ) ;
789806 } catch ( e ) {
790- warn ( `Error while processing event for ${ te . title } ` ) ;
807+ const error = e instanceof Error ? e : new Error ( JSON . stringify ( e ) ) ;
808+ warn ( `Error while processing event for ${ te . title } . ${ error . message } ` ) ;
791809 warn ( e ) ;
792810 }
793811 } ,
0 commit comments