Adding error handling for baanreserveren to take a screenshot on error

This commit is contained in:
Collin Duncan 2024-02-23 07:26:29 -06:00
parent 086950bb56
commit 630d68fa0c
No known key found for this signature in database
GPG key ID: 9584E0870D990D97

View file

@ -8,6 +8,7 @@ import dayjs from '../../common/dayjs'
import { LoggerService } from '../../logger/service.logger' import { LoggerService } from '../../logger/service.logger'
import { Reservation } from '../../reservations/entity' import { Reservation } from '../../reservations/entity'
import { EmptyPage } from '../pages/empty' import { EmptyPage } from '../pages/empty'
import path from 'path'
const BAAN_RESERVEREN_ROOT_URL = 'https://squashcity.baanreserveren.nl' const BAAN_RESERVEREN_ROOT_URL = 'https://squashcity.baanreserveren.nl'
@ -81,6 +82,13 @@ export class BaanReserverenService {
) )
} }
private async handleError() {
await this.page.screenshot({
type: 'png',
path: path.resolve('.', `${Date.now()}_error-screenshot.png`),
}).catch((reason: any) => this.loggerService.warn('Failed to take screenshot', { reason }))
}
private async checkSession(username: string) { private async checkSession(username: string) {
this.loggerService.debug('Checking session', { this.loggerService.debug('Checking session', {
username, username,
@ -465,43 +473,60 @@ export class BaanReserverenService {
} }
public async performReservation(reservation: Reservation) { public async performReservation(reservation: Reservation) {
await this.init(reservation) try {
await this.navigateToDay(reservation.dateRangeStart) await this.init(reservation)
await this.selectAvailableTime(reservation) await this.navigateToDay(reservation.dateRangeStart)
await this.selectOwner(reservation.ownerId) await this.selectAvailableTime(reservation)
await this.selectOpponent(reservation.opponentId, reservation.opponentName) await this.selectOwner(reservation.ownerId)
await this.confirmReservation() await this.selectOpponent(
reservation.opponentId,
reservation.opponentName,
)
await this.confirmReservation()
} catch (error: unknown) {
await this.handleError()
throw error
}
} }
public async addReservationToWaitList(reservation: Reservation) { public async addReservationToWaitList(reservation: Reservation) {
await this.init(reservation) try {
await this.navigateToWaitingList() await this.init(reservation)
const previousWaitingListIds = await this.recordWaitingListEntries() await this.navigateToWaitingList()
await this.openWaitingListDialog() const previousWaitingListIds = await this.recordWaitingListEntries()
await this.inputWaitingListDetails(reservation) await this.openWaitingListDialog()
await this.confirmWaitingListDetails() await this.inputWaitingListDetails(reservation)
await this.navigateToWaitingList() await this.confirmWaitingListDetails()
const currentWaitingListIds = await this.recordWaitingListEntries() await this.navigateToWaitingList()
const waitingListId = this.findNewWaitingListEntryId( const currentWaitingListIds = await this.recordWaitingListEntries()
previousWaitingListIds, const waitingListId = this.findNewWaitingListEntryId(
currentWaitingListIds, previousWaitingListIds,
) currentWaitingListIds,
if (waitingListId == null) {
throw new WaitingListSubmissionError(
'Failed to find new waiting list entry',
) )
}
return waitingListId if (waitingListId == null) {
throw new WaitingListSubmissionError(
'Failed to find new waiting list entry',
)
}
return waitingListId
} catch (error: unknown) {
await this.handleError()
throw error
}
} }
public async removeReservationFromWaitList(reservation: Reservation) { public async removeReservationFromWaitList(reservation: Reservation) {
if (!reservation.waitListed || !reservation.waitingListId) return try {
if (!reservation.waitListed || !reservation.waitingListId) return
await this.init(reservation) await this.init(reservation)
await this.navigateToWaitingList() await this.navigateToWaitingList()
await this.deleteWaitingListEntryRowById(reservation.waitingListId) await this.deleteWaitingListEntryRowById(reservation.waitingListId)
} catch (error: unknown) {
await this.handleError()
throw error
}
} }
} }