Skip to content

Commit

Permalink
fix: handle Steam API error code 500 (#222)
Browse files Browse the repository at this point in the history
* fix: handle Steam API error code 500
  • Loading branch information
garrappachc authored Mar 4, 2020
1 parent b715554 commit e5a2f8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/players/services/steam-api.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Test, TestingModule } from '@nestjs/testing';
import { SteamApiService } from './steam-api.service';
import { of } from 'rxjs';
import { of, throwError } from 'rxjs';
import { readFileSync } from 'fs';
import { resolve } from 'path';
import { HttpService } from '@nestjs/common';
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('SteamApiService', () => {

describe('with response code 500', () => {
beforeEach(() => {
jest.spyOn(httpService, 'get').mockReturnValue(of({ status: 500 } as any));
jest.spyOn(httpService, 'get').mockReturnValue(throwError({ status: 500 } as any));
});

it('should throw an error', async () => {
Expand Down
17 changes: 7 additions & 10 deletions src/players/services/steam-api.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable, HttpService } from '@nestjs/common';
import { Environment } from '@/environment/environment';
import { map, switchMap } from 'rxjs/operators';
import { map, switchMap, catchError } from 'rxjs/operators';
import { floor } from 'lodash';
import { of, throwError } from 'rxjs';

Expand Down Expand Up @@ -29,17 +29,14 @@ export class SteamApiService {
async getTf2InGameHours(steamId64: string): Promise<number> {
return this.httpService.get<UserStatsForGameResponse>(`${this.userStatsForGameEndpoint}/?appid=${this.tf2AppId}&key=${this.environment.steamApiKey}&steamid=${steamId64}&format=json`).pipe(
switchMap(response => {
if (response.status === 200) {
return of(
response.data.playerstats.stats
.filter(s => /\.accum\.iPlayTime$/.test(s.name))
.reduce((sum, curr) => sum + curr.value, 0)
);
} else {
return throwError(new Error('cannot verify in-game hours for TF2'));
}
return of(
response.data.playerstats.stats
.filter(s => /\.accum\.iPlayTime$/.test(s.name))
.reduce((sum, curr) => sum + curr.value, 0)
);
}),
map(seconds => floor(seconds / 60 / 60)),
catchError(() => throwError(new Error('cannot verify in-game hours for TF2'))),
).toPromise();
}

Expand Down

0 comments on commit e5a2f8e

Please sign in to comment.