Skip to content

Commit e0cf5e3

Browse files
committed
add debug file logging to failed API requests
1 parent 821269c commit e0cf5e3

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

src/lib/bluelink-regions/base.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Config } from '../../config'
22
import { defaultImage } from '../../resources/defaultImage'
3+
import PersistedLog from '../scriptable-utils/io/PersistedLog'
34
const KEYCHAIN_CACHE_KEY = 'egmp-bluelink-cache'
45
export const DEFAULT_STATUS_CHECK_INTERVAL = 3600 * 1000
6+
const BLUELINK_LOG_FILE = 'egmp-bluelink-log'
57

68
export interface BluelinkTokens {
79
accessToken: string
@@ -92,6 +94,7 @@ export class Bluelink {
9294
protected tempLookup: TempConversion | undefined
9395
protected tokens: BluelinkTokens | undefined
9496
protected debugLastRequest: DebugLastRequest | undefined
97+
protected logger: any
9598

9699
constructor(config: Config, vin?: string) {
97100
this.vin = vin
@@ -101,6 +104,7 @@ export class Bluelink {
101104
this.tokens = undefined
102105
this.debugLastRequest = undefined
103106
this.tempLookup = undefined
107+
this.logger = PersistedLog(BLUELINK_LOG_FILE)
104108
}
105109

106110
protected async superInit(config: Config, vin?: string, statusCheckInterval?: number) {
@@ -269,6 +273,7 @@ export class Bluelink {
269273
const json = await req.loadJSON()
270274
return { resp: req.response, json: json }
271275
} catch (error) {
276+
await this.logger.log(`Failed to send request to ${props.url}, error ${error}`)
272277
throw Error(`Failed to send request to ${props.url}, error ${error}`)
273278
}
274279
}

src/lib/bluelink-regions/canada.ts

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,10 @@ export class BluelinkCanada extends Bluelink {
7878
expiry: Math.floor(Date.now() / 1000) + resp.json.result.token.expireIn, // we only get a expireIn not a actual date
7979
}
8080
}
81-
throw Error(`Login Failed: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`)
81+
82+
const error = `Login Failed: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
83+
await this.logger.log(error)
84+
throw Error(error)
8285
}
8386

8487
protected async setCar(id: string) {
@@ -89,9 +92,9 @@ export class BluelinkCanada extends Bluelink {
8992
}),
9093
})
9194
if (!this.requestResponseValid(resp.json)) {
92-
throw Error(
93-
`Failed to set car ${id}: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
94-
)
95+
const error = `Failed to set car ${id}: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
96+
await this.logger.log(error)
97+
throw Error(error)
9598
}
9699
}
97100

@@ -122,9 +125,9 @@ export class BluelinkCanada extends Bluelink {
122125
modelTrim: vehicle.trim,
123126
}
124127
}
125-
throw Error(
126-
`Failed to retrieve vehicle list: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
127-
)
128+
const error = `Failed to retrieve vehicle list: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
129+
await this.logger.log(error)
130+
throw Error(error)
128131
}
129132

130133
protected returnCarStatus(status: any, forceUpdate: boolean, odometer?: number): BluelinkStatus {
@@ -199,9 +202,9 @@ export class BluelinkCanada extends Bluelink {
199202
: this.returnCarStatus(resp.json.result.status, forceUpdate, resp.json.result.vehicle.odometer)
200203
}
201204

202-
throw Error(
203-
`Failed to retrieve vehicle status: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
204-
)
205+
const error = `Failed to retrieve vehicle status: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
206+
await this.logger.log(error)
207+
throw Error(error)
205208
}
206209

207210
protected async getAuthCode(): Promise<string> {
@@ -216,9 +219,9 @@ export class BluelinkCanada extends Bluelink {
216219
if (this.requestResponseValid(resp.json)) {
217220
return resp.json.result.pAuth
218221
}
219-
throw Error(
220-
`Failed to get auth code: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
221-
)
222+
const error = `Failed to get auth code: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
223+
await this.logger.log(error)
224+
throw Error(error)
222225
}
223226

224227
protected async pollForCommandCompletion(
@@ -240,9 +243,9 @@ export class BluelinkCanada extends Bluelink {
240243
})
241244

242245
if (!this.requestResponseValid(resp.json)) {
243-
throw Error(
244-
`Failed to poll for command completion: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
245-
)
246+
const error = `Failed to poll for command completion: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
247+
await this.logger.log(error)
248+
throw Error(error)
246249
}
247250

248251
if (resp.json.result.transaction.apiResult === 'C') {
@@ -291,9 +294,9 @@ export class BluelinkCanada extends Bluelink {
291294
const transactionId = resp.resp.headers.transactionId
292295
return await this.pollForCommandCompletion(id, authCode, transactionId)
293296
}
294-
throw Error(
295-
`Failed to send lockUnlock command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
296-
)
297+
const error = `Failed to send lockUnlock command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
298+
await this.logger.log(error)
299+
throw Error(error)
297300
}
298301

299302
protected async startCharge(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
@@ -325,9 +328,9 @@ export class BluelinkCanada extends Bluelink {
325328
const transactionId = resp.resp.headers.transactionId
326329
return await this.pollForCommandCompletion(id, authCode, transactionId)
327330
}
328-
throw Error(
329-
`Failed to send charge command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
330-
)
331+
const error = `Failed to send charge command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
332+
await this.logger.log(error)
333+
throw Error(error)
331334
}
332335

333336
protected async climateOn(id: string, config: ClimateRequest): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
@@ -368,9 +371,9 @@ export class BluelinkCanada extends Bluelink {
368371
const transactionId = resp.resp.headers.transactionId
369372
return await this.pollForCommandCompletion(id, authCode, transactionId)
370373
}
371-
throw Error(
372-
`Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
373-
)
374+
const error = `Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
375+
await this.logger.log(error)
376+
throw Error(error)
374377
}
375378

376379
protected async climateOff(id: string): Promise<{ isSuccess: boolean; data: BluelinkStatus }> {
@@ -391,8 +394,8 @@ export class BluelinkCanada extends Bluelink {
391394
const transactionId = resp.resp.headers.transactionId
392395
return await this.pollForCommandCompletion(id, authCode, transactionId)
393396
}
394-
throw Error(
395-
`Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`,
396-
)
397+
const error = `Failed to send climateOff command: ${JSON.stringify(resp.json)} request ${JSON.stringify(this.debugLastRequest)}`
398+
await this.logger.log(error)
399+
throw Error(error)
397400
}
398401
}

0 commit comments

Comments
 (0)