Skip to content

Commit 30bf7aa

Browse files
committed
fix(memfault): do not log missing device as error
1 parent 1a2bb02 commit 30bf7aa

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Memfault/api.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { validateWithTypeBox } from '@hello.nrfcloud.com/proto'
22
import { type Static } from '@sinclair/typebox'
3+
import { NotFoundError } from '../util/NotFoundError.js'
34
import { ValidationError } from '../util/ValidationError.js'
45
import { MemfaultReboots } from './MemfaultReboots.js'
56

@@ -42,12 +43,20 @@ export const getDeviceReboots =
4243
}),
4344
},
4445
)
45-
if (!res.ok)
46+
if (!res.ok) {
47+
if (res.status === 404) {
48+
return {
49+
error: new NotFoundError(
50+
`Failed to fetch reboots: ${res.status}. ${await res.text()}`,
51+
),
52+
}
53+
}
4654
return {
4755
error: new Error(
4856
`Failed to fetch reboots: ${res.status}. ${await res.text()}`,
4957
),
5058
}
59+
}
5160

5261
const maybeValid = v(await res.json())
5362
if ('errors' in maybeValid)

lambda/memfault/fetchReboots.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { fromEnv } from '@bifravst/from-env'
99
import { logger } from '@hello.nrfcloud.com/lambda-helpers/logger'
1010
import { metricsForComponent } from '@hello.nrfcloud.com/lambda-helpers/metrics'
1111
import { requestLogger } from '@hello.nrfcloud.com/lambda-helpers/requestLogger'
12+
import { NotFoundError } from '@hello.nrfcloud.com/proto/hello'
1213
import middy from '@middy/core'
1314
import type { SQSEvent } from 'aws-lambda'
1415
import { getDeviceReboots } from '../../Memfault/api.js'
@@ -61,13 +62,16 @@ const h = async (event: SQSEvent): Promise<void> => {
6162

6263
if (deviceId === undefined || since === undefined) {
6364
log.error('Missing required attributes')
64-
track('error', MetricUnit.Count, 1)
6565
continue
6666
}
6767
const maybeReboots = await fetchReboots(deviceId, since)
6868
if ('error' in maybeReboots) {
69-
log.error(maybeReboots.error.message)
70-
track('error', MetricUnit.Count, 1)
69+
if (maybeReboots.error instanceof NotFoundError) {
70+
log.debug(maybeReboots.error.message)
71+
} else {
72+
log.error(maybeReboots.error.message)
73+
track('error', MetricUnit.Count, 1)
74+
}
7175
continue
7276
}
7377
const reboots = maybeReboots.value.data

util/NotFoundError.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export class NotFoundError extends Error {
2+
constructor(message: string) {
3+
super(message)
4+
this.name = 'NotFoundError'
5+
}
6+
}

0 commit comments

Comments
 (0)