Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

Properly handle integration API exceptions

Blue Mouse35c9e117c9be
diff --git a/apps/production/src/user/user.controller.ts b/apps/production/src/user/user.controller.ts
index 67edd5483..95915cbfe 100644
--- a/apps/production/src/user/user.controller.ts
+++ b/apps/production/src/user/user.controller.ts
@@ -32,7 +32,7 @@ import * as _omit from 'lodash/omit'
import * as _round from 'lodash/round'
import { v4 as uuidv4 } from 'uuid'
import { HttpService } from '@nestjs/axios'
-import { firstValueFrom, map } from 'rxjs'
+import { catchError, firstValueFrom, map, of } from 'rxjs'
import { Markup } from 'telegraf'
import { JwtAccessTokenGuard } from '../auth/guards'
@@ -652,18 +652,21 @@ export class UserController {
if (userDTO.slackWebhookUrl) {
const slackWebhookResponse = await firstValueFrom(
- this.httpService
- .get<string>(userDTO.discordWebhookUrl)
- .pipe(map(response => response.data)),
+ this.httpService.get<string>(userDTO.slackWebhookUrl).pipe(
+ map(response => response.data),
+ catchError(error => {
+ if (error.response && error.response.data) {
+ return of(error.response.data)
+ }
+
+ return of('Error occurred, but no response data was returned')
+ }),
+ ),
)
if (slackWebhookResponse === 'invalid_token') {
throw new ConflictException('Invalid Slack URL.')
}
-
- await this.userService.update(id, {
- slackWebhookUrl: userDTO.slackWebhookUrl,
- })
} else if (userDTO.slackWebhookUrl === null) {
await this.userService.update(id, {
slackWebhookUrl: null,
@@ -674,16 +677,21 @@ export class UserController {
const discordWebhookResponse = await firstValueFrom(
this.httpService
.get<{ code?: number }>(userDTO.discordWebhookUrl)
- .pipe(map(response => response.data)),
+ .pipe(
+ map(response => response.data),
+ catchError(error => {
+ if (error.response && error.response.data) {
+ return of(error.response.data)
+ }
+
+ return of('Error occurred, but no response data was returned')
+ }),
+ ),
)
if (discordWebhookResponse.code === 50027) {
throw new ConflictException('Invalid Discord URL.')
}
-
- await this.userService.update(id, {
- discordWebhookUrl: userDTO.discordWebhookUrl,
- })
} else if (userDTO.discordWebhookUrl === null) {
await this.userService.update(id, {
discordWebhookUrl: null,
@@ -751,8 +759,8 @@ export class UserController {
const updatedUser = await this.userService.findOneWhere({ id })
return this.userService.omitSensitiveData(updatedUser)
- } catch (e) {
- throw new BadRequestException(e.message)
+ } catch (reason) {
+ throw new BadRequestException(reason.message)
}
}