Skip to content
This repository was archived by the owner on Jun 2, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions node/lib/OpenT2TConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
*/
export class OpenT2TConstants {
// Errors
public static InternalServerError: string =
"Unexpected error. Please retry the operation or contact OpenT2T support";

public static InternalServerError: string = "Unexpected error. Please retry the operation or contact OpenT2T support";
public static AccessDenied: string = "Access denied";
public static DeviceNotFound: string = "Device not found";
public static HMacSignatureVerificationFailed: string = "Payload signature doesn't match";
public static InvalidAuthInfoInput: string =
"Invalid authInfo object. Please provide the existing authInfo object";
public static InvalidAuthInfoInput: string = "Invalid authInfo object. Please provide the existing authInfo object";
public static ExpiredAuthToken: string = "Expired authentication token";
public static InvalidHubId: string = "Invalid hub id";
public static InvalidResourceId: string = "Invalid resourceId";
public static InvalidResource: string = "Invalid resource";
public static MissingTranslator: string = "Cannot find translator module";
public static MustSubscribeToDevice: string = "Must subscribe to a device";
public static NotImplemented: string = "Not Implemented";
public static Unreachable: string = "Device is unreachable";
public static DeviceOff: string = "Device is off";
public static RangeCheck: string = "Range check failed";
public static ResourceNotFound: string = "Resource not found";
public static UnknownHubSubscribeRequest: string = "Unknown subscription request";
public static UnknownPlatform: string = "Unknown platform. The requested platform cannot be translated";
Expand Down
39 changes: 38 additions & 1 deletion node/lib/OpenT2TError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { OpenT2TConstants } from "./OpenT2TConstants";
import { OpenT2TErrorData } from "./OpenT2TErrorData";

/**
* Custom Error class that extends built-in Error.
Expand All @@ -7,11 +8,47 @@ export class OpenT2TError extends Error {
public statusCode: number;
public innerError: Error;

constructor(statusCode: number, message: string, innerError?: Error) {
constructor(statusCode: number, message: string, innerError?: Error, data?: OpenT2TErrorData) {
if (!message) {
message = OpenT2TConstants.InternalServerError;
}

//**** Format the message if the user provided data */
// Bad request
if (data !== undefined && data !== null) {
if (statusCode == 400) {
// Invalid resource
if (message === OpenT2TConstants.InvalidResource) {
message = data.provider + ": " + message + " found " + data.attributeId + " '" + data.value + "'";
if(data.expected) {
message += "; expected " + data.expected;
}
}
}
if (statusCode == 401) {
// Unauthorized
if (message === OpenT2TConstants.AccessDenied) {
message = data.provider + ": " + message;
if(data.details) {
message += "; " + data.details;
}
}
}
if (statusCode == 440) {
// Value outside range
if (message === OpenT2TConstants.RangeCheck) {
message = data.provider + ": " + message + " found " + data.attributeId + " '" + data.value
+ " " + data.unit + "'; expected [" + data.min + ", " + data.max + "]";
}
}
if (statusCode == 442) {
// Device is off
if (message === OpenT2TConstants.DeviceOff) {
message = data.provider + ": " + message;
}
}
}

super(message);

// Set the prototype explicitly due to TS breaking change.
Expand Down
14 changes: 14 additions & 0 deletions node/lib/OpenT2TErrorData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export class OpenT2TErrorData {

public provider: string;
public attributeId: string;
public value: string;
public unit: string;

public min: number;
public max: number;

public expected: string;
public details: string;

}