Skip to content

Commit 556f3b3

Browse files
committed
Improve data field message
1 parent de24c15 commit 556f3b3

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src/data-field-strategy.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { Converters } from "./converters";
12
import { getInputChecked } from "./dom";
23

34
export interface DataFieldStrategy {
@@ -146,6 +147,7 @@ export class Exception {
146147
throw new Error('Just on byte of exception code is allowed!');
147148
}
148149
this.exceptionCode = data[0];
149-
this.exceptionDescription = Exception.exceptionDescriptions.get(this.exceptionCode);
150+
this.exceptionDescription = Exception.exceptionDescriptions.get(this.exceptionCode) ??
151+
`0x${Converters.byteToHex(this.exceptionCode)} => UNKNOWN EXCEPTION CODE`;
150152
}
151153
}

src/frame.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,21 +45,31 @@ export class Frame {
4545
private getDataAsText(): string {
4646
if (this.type === 'error') {
4747
return `Invalid frame: 0x${this.hexData}`;
48-
} else if (this.isUnknownFunctionCode()) {
48+
} else if (this.isUnknownFunctionCodeStrategy()) {
4949
return `No strategy to format data field. Raw data field is: 0x${this.hexData}`;
5050
} else if (this.isNoValidDataFormat()) {
51-
return `This frame format does not fit to any known strategies: masterRequest=${this.masterRequest?.error}; slaveResponse=${this.slaveResponse?.error}; raw data: 0x${this.hexData}`;
51+
return `This frame format does not fit to any known strategies:
52+
${Frame.conditionalText(!!this.masterRequest?.error, `masterRequestError=${this.masterRequest?.error}; `)}
53+
${Frame.conditionalText(!!this.slaveResponse?.error, `slaveResponseError=${this.slaveResponse?.error}; `)}
54+
raw data: 0x${this.hexData}`;
5255
} else {
53-
return `Valid frame: masterRequest=${JSON.stringify(this.masterRequest?.object)}; slaveResponse=${JSON.stringify(this.slaveResponse?.object)}`;
56+
return `Valid frame:
57+
${Frame.conditionalText(!!this.masterRequest?.object, `masterRequest=${JSON.stringify(this.masterRequest?.object)}; `)}
58+
${Frame.conditionalText(!!this.slaveResponse?.object, `slaveResponse=${JSON.stringify(this.slaveResponse?.object)}; `)}
59+
`;
5460
}
5561
}
5662

57-
private isUnknownFunctionCode(): boolean {
63+
private static conditionalText(condition: boolean, value: string): string {
64+
return condition ? value : '';
65+
}
66+
67+
private isUnknownFunctionCodeStrategy(): boolean {
5868
return !this.masterRequest && !this.slaveResponse;
5969
}
6070

6171
private isNoValidDataFormat(): boolean {
62-
return (!this.masterRequest || !!this.masterRequest?.error) && (!this.slaveResponse || !!this.slaveResponse?.error);
72+
return !this.masterRequest?.object && !this.slaveResponse?.object;
6373
}
6474

6575
protected getError(e: any): string {

0 commit comments

Comments
 (0)