Skip to content

Commit

Permalink
Fix blackout check (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasodonnell authored Jul 29, 2024
1 parent 8e09518 commit 74c416b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 24 deletions.
16 changes: 3 additions & 13 deletions src/clients/mediagateway/mediagateway.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosInstance, AxiosResponse } from 'axios'

import { HttpException, Injectable } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import axios, { AxiosError } from 'axios'

Expand Down Expand Up @@ -102,16 +102,9 @@ export class MediaGatewayService {

return data.initPlaybackSession
} catch (cause) {
if (cause instanceof AxiosError) {
throw new HttpException(`Failed to init playback session`, cause.response?.status ?? 500, { cause })
}

if (cause instanceof HttpException) {
throw cause
}

throw new ApplicationException(`Failed to init playback session`, {
cause,
status: cause instanceof AxiosError ? cause.response?.status : undefined,
})
}
}
Expand Down Expand Up @@ -173,12 +166,9 @@ export class MediaGatewayService {

return data.initSession
} catch (cause) {
if (cause instanceof AxiosError) {
throw new HttpException(`Failed to initialize session`, cause.response?.status ?? 500, { cause })
}

throw new ApplicationException(`Failed to initialize session`, {
cause,
status: cause instanceof AxiosError ? cause.response?.status : undefined,
})
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/clients/mlbauth/mlbauth.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosInstance, AxiosResponse } from 'axios'

import { HttpException, Injectable } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import axios, { AxiosError } from 'axios'

Expand Down Expand Up @@ -43,12 +43,9 @@ export class MlbAuthService {

return response.data
} catch (cause) {
if (cause instanceof AxiosError) {
throw new HttpException(`Failed to fetch grant`, cause.response?.status ?? 500, { cause })
}

throw new ApplicationException(`Failed to fetch grant`, {
cause,
status: cause instanceof AxiosError ? cause.response?.status : undefined,
})
}
}
Expand Down
7 changes: 2 additions & 5 deletions src/clients/mlbstatic/mlbstatic.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { AxiosInstance, AxiosResponse } from 'axios'

import { HttpException, Injectable } from '@nestjs/common'
import { Injectable } from '@nestjs/common'
import { ConfigService } from '@nestjs/config'
import axios, { AxiosError } from 'axios'

Expand All @@ -25,12 +25,9 @@ export class MlbStaticService {

return response.data
} catch (cause) {
if (cause instanceof AxiosError) {
throw new HttpException(`Failed to fetch logo`, cause.response?.status ?? 500, { cause })
}

throw new ApplicationException(`Failed to fetch logo`, {
cause,
status: cause instanceof AxiosError ? cause.response?.status : undefined,
})
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/common/errors/application.error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export class ApplicationException extends Error {
this.meta = meta
}

public getFullMessage(): string {
if (this.cause instanceof ApplicationException) {
return `${this.message}: ${this.cause.getFullMessage()}`
}

if (this.cause instanceof Error) {
return `${this.message}: ${this.cause.message}`
}

return this.message
}

public getMeta(): Record<string, any> {
return this.meta
}
Expand Down
3 changes: 2 additions & 1 deletion src/features/stream/stream.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import axios from 'axios'
import { Bitrate, BlackedOutException, Stream, StreamEntity, StreamException } from './types'

import { MediaGatewayService, Playback } from '@/clients/mediagateway'
import { ApplicationException } from '@/common/errors'
import { Token } from '@/features/auth'
import { Game } from '@/features/game'
import { Session } from '@/features/session'
Expand Down Expand Up @@ -109,7 +110,7 @@ export class StreamService {
url: stream.url,
}
} catch (cause) {
if (cause instanceof Error && cause.message.includes('blackout')) {
if (cause instanceof ApplicationException && cause.getFullMessage().includes('blacked out')) {
this.logger.warn({ ...game }, 'Game is blacked out')
throw new BlackedOutException('Game is blacked out', { ...game })
}
Expand Down

0 comments on commit 74c416b

Please sign in to comment.