Check for ApiError before using it as such

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
Cameron Diver 2020-05-26 13:53:38 +01:00
parent 68b2b1be0b
commit 972b896c95
6 changed files with 20 additions and 6 deletions

6
package-lock.json generated
View File

@ -1606,9 +1606,9 @@
"integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c="
},
"balena-register-device": {
"version": "6.1.5",
"resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-6.1.5.tgz",
"integrity": "sha512-fQ2KCCSW+igWb79y1UiJgLjtiZhaKHVj1iR4RajPFE4EuMEa03LP0bMLVuqVBe1ggcYoyfAaKmIFLoGokYOhGw==",
"version": "6.1.6",
"resolved": "https://registry.npmjs.org/balena-register-device/-/balena-register-device-6.1.6.tgz",
"integrity": "sha512-kS8JZoLyucZ9oUFicspN/k3jRrvdtQ4UYXVCHHyw91C3y2Z1T5CQpwfMBSqA8dp5wQKyP527K/0+lMUa2ncLhA==",
"dev": true,
"requires": {
"bluebird": "^3.7.2",

View File

@ -62,7 +62,7 @@
"@types/supertest": "^2.0.9",
"@types/tmp": "^0.1.0",
"@types/yargs": "^15.0.4",
"balena-register-device": "^6.1.5",
"balena-register-device": "^6.1.6",
"blinking": "~0.0.3",
"bluebird": "^3.7.2",
"body-parser": "^1.19.0",

View File

@ -868,7 +868,10 @@ export class APIBinder {
try {
device = await deviceRegister.register(opts).timeout(opts.apiTimeout);
} catch (err) {
if (isHttpConflictError(err.response)) {
if (
err instanceof deviceRegister.ApiError &&
isHttpConflictError(err.response)
) {
log.debug('UUID already registered, trying a key exchange');
device = await this.exchangeKeyAndGetDeviceOrRegenerate(opts);
} else {

View File

@ -1,5 +1,6 @@
import { endsWith, map } from 'lodash';
import TypedError = require('typed-error');
import { Response } from 'request';
import { checkInt } from './validation';
@ -45,7 +46,7 @@ export class InvalidAppIdError extends TypedError {
export class UpdatesLockedError extends TypedError {}
export function isHttpConflictError(err: StatusCodeError): boolean {
export function isHttpConflictError(err: StatusCodeError | Response): boolean {
return checkInt(err.statusCode) === 409;
}

View File

@ -2,6 +2,8 @@ import factory = require('balena-register-device');
import * as Bluebird from 'bluebird';
import { getRequestInstance } from './request';
export const { ApiError } = factory;
export const { generateUniqueKey, register } = factory({
request: {
send: Bluebird.method(async (options: {}) => {

View File

@ -1,11 +1,19 @@
// Typings (incomplete) for balena-register-device@v6.0.1
// TODO: Upstream types to the repo
declare module 'balena-register-device' {
import TypedError = require('typed-error');
import { Response } from 'request';
function factory({
request,
}): {
generateUniqueKey: () => string;
register: (opts: Dictionary<any>) => Bluebird<{ id: string }>;
};
factory.ApiError = class ApiError extends TypedError {
response: Response;
};
export = factory;
}