Skip to content

Commit

Permalink
feat: Updating error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kxc171 committed Jul 9, 2024
1 parent ace04cb commit 6e01612
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 26 deletions.
8 changes: 6 additions & 2 deletions __tests__/dynatrace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ describe('dynatrace', () => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const result = () => dt.metric2line(metric)
expect(result).toThrow(Error)
expect(result).toThrow('Unsupported Metric format bad-format')
expect(result).toThrow(
"Unsupported Metric format for 'bad.metric' - bad-format"
)
})

it('converts an Event to its payload', async () => {
Expand Down Expand Up @@ -105,6 +107,8 @@ describe('dynatrace', () => {
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const result = () => dt.event2payload(event)
expect(result).toThrow(Error)
expect(result).toThrow('Unsupported Event type CUSTOM_INVALID')
expect(result).toThrow(
"Unsupported Event type for 'Bad Event' - CUSTOM_INVALID"
)
})
})
2 changes: 1 addition & 1 deletion badges/coverage.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 24 additions & 9 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

37 changes: 24 additions & 13 deletions src/dynatrace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export function metric2line(metric: Metric): string {
if (SUPPORTED_METRIC_FORMATS.includes(metric.format)) {
line += ` ${metric.format},${metric.value}`
} else {
throw Error(`Unsupported Metric format ${metric.format}`)
throw Error(
`Unsupported Metric format for '${metric.metric}' - ${metric.format}`
)
}
} else line += ` ${metric.value}`

Expand Down Expand Up @@ -108,7 +110,7 @@ export function event2payload(event: Event): {

return payload
} else {
throw Error(`Unsupported Event type ${event.type}`)
throw Error(`Unsupported Event type for '${event.title}' - ${event.type}`)
}
}

Expand All @@ -121,11 +123,18 @@ export async function sendMetrics(

const lines: string[] = []
for (const metric of metrics) {
const line = metric2line(metric)
core.info(line)
lines.push(line)
try {
const line = metric2line(metric)
core.info(line)
lines.push(line)
} catch (error) {
core.setFailed((error as Error).message)
}
}

// skip if no valid metrics are present
if (lines.length === 0) return

const http: httpm.HttpClient = getClient(token, 'text/plain')
const res: httpm.HttpClientResponse = await http.post(
`${url}/api/v2/metrics/ingest`,
Expand All @@ -134,9 +143,7 @@ export async function sendMetrics(

core.info(await res.readBody())
if (res.message.statusCode !== 202) {
throw Error(
`HTTP request failed with status code: ${res.message.statusCode}`
)
core.setFailed(`HTTP request failed - ${res.message.statusCode}`)
}
}

Expand All @@ -147,10 +154,16 @@ export async function sendEvents(
): Promise<void> {
core.info(`Sending ${events.length} event(s)`)

let payload = {}
for (const event of events) {
const payload = event2payload(event)
try {
payload = event2payload(event)
core.info(JSON.stringify(payload))
} catch (error) {
core.setFailed((error as Error).message)
continue
}

core.info(JSON.stringify(payload))
const http: httpm.HttpClient = getClient(token, 'application/json')
const res: httpm.HttpClientResponse = await http.post(
`${url}/api/v2/events/ingest`,
Expand All @@ -159,9 +172,7 @@ export async function sendEvents(

core.info(await res.readBody())
if (res.message.statusCode !== 201) {
throw Error(
`HTTP request failed with status code: ${res.message.statusCode})}`
)
core.setFailed(`HTTP request failed - ${res.message.statusCode}`)
}
}
}
Expand Down

0 comments on commit 6e01612

Please sign in to comment.