Skip to content

Commit

Permalink
feat: move usb errors inside model folder
Browse files Browse the repository at this point in the history
  • Loading branch information
aussedatlo committed Feb 14, 2024
1 parent de3266a commit c9226d3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 71 deletions.
63 changes: 63 additions & 0 deletions packages/core/src/internal/usb/model/Errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
export type PromptDeviceAccessError =
| UsbHidTransportNotSupportedError
| NoAccessibleDeviceError;

export type ConnectError = UnknownDeviceError | OpeningConnectionError;

export type exchangeApduError = UnconnectedDeviceError | SendingApduError;

export class DeviceNotRecognizedError {
readonly _tag = "DeviceNotRecognizedError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class NoAccessibleDeviceError {
readonly _tag = "NoAccessibleDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class OpeningConnectionError {
readonly _tag = "ConnectionOpeningError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class SendingApduError {
readonly _tag = "SendingApduError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class UnconnectedDeviceError {
readonly _tag = "UnconnectedDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class UnknownDeviceError {
readonly _tag = "UnknownDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class UsbHidTransportNotSupportedError {
readonly _tag = "UsbHidTransportNotSupportedError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}
67 changes: 1 addition & 66 deletions packages/core/src/internal/usb/transport/UsbHidTransport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,72 +4,7 @@ import { Observable } from "rxjs";
import { DeviceId } from "@internal/device-model/model/DeviceModel";
import { ConnectedDevice } from "@internal/usb/model/ConnectedDevice";
import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice";

export class UsbHidTransportNotSupportedError {
readonly _tag = "UsbHidTransportNotSupportedError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}
// [ASK] I feel it's easier to read if we define the errors that can occur from the
// transport/datasource inside the file where the associated interface is defined
export class NoAccessibleDeviceError {
readonly _tag = "NoAccessibleDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

// [Ask] naming: Error or Failure suffix ?
export type PromptDeviceAccessError =
| UsbHidTransportNotSupportedError
| NoAccessibleDeviceError;

export class DeviceNotRecognizedError {
readonly _tag = "DeviceNotRecognizedError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class UnknownDeviceError {
readonly _tag = "UnknownDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class OpeningConnectionError {
readonly _tag = "ConnectionOpeningError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export type ConnectError = UnknownDeviceError | OpeningConnectionError;

export class UnconnectedDeviceError {
readonly _tag = "UnconnectedDeviceError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export class SendingApduError {
readonly _tag = "SendingApduError";
originalError?: Error;
constructor(readonly err?: Error) {
this.originalError = err;
}
}

export type exchangeApduError = UnconnectedDeviceError | SendingApduError;
import { ConnectError } from "@internal/usb/model/Errors";

/**
* Transport interface representing a USB HID communication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
import { InMemoryDeviceModelDataSource } from "@internal/device-model/data/InMemoryDeviceModelDataSource";
import { DeviceModelId } from "@internal/device-model/model/DeviceModel";

import {
NoAccessibleDeviceError,
UsbHidTransportNotSupportedError,
} from "./UsbHidTransport";
} from "@internal/usb/model/Errors";

import { WebUsbHidTransport } from "./WebUsbHidTransport";

// Our InMemoryDeviceModelDataSource can directly be used in our unit tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { DeviceId } from "@internal/device-model/model/DeviceModel";
import { ledgerVendorId } from "@internal/usb/data/UsbHidConfig";
import { ConnectedDevice } from "@internal/usb/model/ConnectedDevice";
import { DiscoveredDevice } from "@internal/usb/model/DiscoveredDevice";

import {
ConnectError,
DeviceNotRecognizedError,
Expand All @@ -19,9 +18,10 @@ import {
SendingApduError,
UnconnectedDeviceError,
UnknownDeviceError,
type UsbHidTransport,
UsbHidTransportNotSupportedError,
} from "./UsbHidTransport";
} from "@internal/usb/model/Errors";

import { UsbHidTransport } from "./UsbHidTransport";

// An attempt to manage the state of several devices with one transport. Not final.
type WebHidInternalDevice = {
Expand Down

0 comments on commit c9226d3

Please sign in to comment.