Skip to content

Commit

Permalink
Improve data field message
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianscheit committed Feb 7, 2025
1 parent de24c15 commit 556f3b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/data-field-strategy.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Converters } from "./converters";
import { getInputChecked } from "./dom";

export interface DataFieldStrategy {
Expand Down Expand Up @@ -146,6 +147,7 @@ export class Exception {
throw new Error('Just on byte of exception code is allowed!');
}
this.exceptionCode = data[0];
this.exceptionDescription = Exception.exceptionDescriptions.get(this.exceptionCode);
this.exceptionDescription = Exception.exceptionDescriptions.get(this.exceptionCode) ??
`0x${Converters.byteToHex(this.exceptionCode)} => UNKNOWN EXCEPTION CODE`;
}
}
20 changes: 15 additions & 5 deletions src/frame.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,31 @@ export class Frame {
private getDataAsText(): string {
if (this.type === 'error') {
return `Invalid frame: 0x${this.hexData}`;
} else if (this.isUnknownFunctionCode()) {
} else if (this.isUnknownFunctionCodeStrategy()) {
return `No strategy to format data field. Raw data field is: 0x${this.hexData}`;
} else if (this.isNoValidDataFormat()) {
return `This frame format does not fit to any known strategies: masterRequest=${this.masterRequest?.error}; slaveResponse=${this.slaveResponse?.error}; raw data: 0x${this.hexData}`;
return `This frame format does not fit to any known strategies:
${Frame.conditionalText(!!this.masterRequest?.error, `masterRequestError=${this.masterRequest?.error}; `)}
${Frame.conditionalText(!!this.slaveResponse?.error, `slaveResponseError=${this.slaveResponse?.error}; `)}
raw data: 0x${this.hexData}`;
} else {
return `Valid frame: masterRequest=${JSON.stringify(this.masterRequest?.object)}; slaveResponse=${JSON.stringify(this.slaveResponse?.object)}`;
return `Valid frame:
${Frame.conditionalText(!!this.masterRequest?.object, `masterRequest=${JSON.stringify(this.masterRequest?.object)}; `)}
${Frame.conditionalText(!!this.slaveResponse?.object, `slaveResponse=${JSON.stringify(this.slaveResponse?.object)}; `)}
`;
}
}

private isUnknownFunctionCode(): boolean {
private static conditionalText(condition: boolean, value: string): string {
return condition ? value : '';
}

private isUnknownFunctionCodeStrategy(): boolean {
return !this.masterRequest && !this.slaveResponse;
}

private isNoValidDataFormat(): boolean {
return (!this.masterRequest || !!this.masterRequest?.error) && (!this.slaveResponse || !!this.slaveResponse?.error);
return !this.masterRequest?.object && !this.slaveResponse?.object;
}

protected getError(e: any): string {
Expand Down

0 comments on commit 556f3b3

Please sign in to comment.