Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ErrorInfo.fromValues’s signature correspond to its expectations #1322

Merged
Merged
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
4 changes: 2 additions & 2 deletions src/common/lib/types/devicedetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Utils from '../util/utils';
import ErrorInfo from './errorinfo';
import ErrorInfo, { IConvertibleToErrorInfo } from './errorinfo';

enum DeviceFormFactor {
Phone = 'phone',
Expand Down Expand Up @@ -88,7 +88,7 @@ class DeviceDetails {
}

static fromValues(values: Record<string, unknown>): DeviceDetails {
values.error = values.error && ErrorInfo.fromValues(values.error as Error);
values.error = values.error && ErrorInfo.fromValues(values.error as IConvertibleToErrorInfo);
return Object.assign(new DeviceDetails(), values);
}

Expand Down
10 changes: 8 additions & 2 deletions src/common/lib/types/errorinfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ function toString(err: ErrorInfo | PartialErrorInfo) {
return result;
}

export interface IConvertibleToErrorInfo {
message: string;
code: number;
statusCode: number;
}

export default class ErrorInfo extends Error implements IPartialErrorInfo, API.Types.ErrorInfo {
code: number;
statusCode: number;
Expand All @@ -40,8 +46,8 @@ export default class ErrorInfo extends Error implements IPartialErrorInfo, API.T
return toString(this);
}

static fromValues(values: Record<string, unknown> | ErrorInfo | Error): ErrorInfo {
const { message, code, statusCode } = values as ErrorInfo;
static fromValues(values: IConvertibleToErrorInfo): ErrorInfo {
const { message, code, statusCode } = values;
if (typeof message !== 'string' || typeof code !== 'number' || typeof statusCode !== 'number') {
throw new Error('ErrorInfo.fromValues(): invalid values: ' + Platform.Config.inspect(values));
}
Expand Down
5 changes: 4 additions & 1 deletion src/platform/nodejs/lib/util/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,10 @@ var CryptoFactory = function (bufferUtils: typeof BufferUtils) {

generateRandom((keyLength || DEFAULT_KEYLENGTH) / 8, function (err, buf) {
if (callback !== undefined) {
callback(err ? ErrorInfo.fromValues(err) : null, buf);
const errorInfo = err
? new ErrorInfo('Failed to generate random key: ' + err.message, 500, 50000, err)
: null;
callback(errorInfo, buf);
}
});
}
Expand Down
5 changes: 4 additions & 1 deletion src/platform/web/lib/util/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ var CryptoFactory = function (config: IPlatformConfig, bufferUtils: typeof Buffe

generateRandom((keyLength || DEFAULT_KEYLENGTH) / 8, function (err, buf) {
if (callback !== undefined) {
callback(err ? ErrorInfo.fromValues(err) : null, buf ?? undefined);
const errorInfo = err
? new ErrorInfo('Failed to generate random key: ' + err.message, 400, 50000, err)
: null;
callback(errorInfo, buf ?? undefined);
}
});
}
Expand Down