Skip to content

Commit

Permalink
# Throw non-ResponseError errors
Browse files Browse the repository at this point in the history
Signed-off-by: Theo Truong <theotr@amazon.com>
  • Loading branch information
nhtruong committed May 27, 2024
1 parent 5c6aace commit c566451
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
5 changes: 3 additions & 2 deletions tools/src/tester/ChapterEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { type Chapter, type ActualResponse } from './types/story.types'
import { type ChapterEvaluation, type Evaluation, Result } from './types/eval.types'
import { type ParsedOperation } from './types/spec.types'
import { overall_result } from './helpers'
import { ResponseError } from './ChapterReader'

export default class ChapterEvaluator {
chapter: Chapter
Expand All @@ -28,8 +29,8 @@ export default class ChapterEvaluator {
result: overall_result(Object.values(params).concat([request_body, status, payload]))
}
} catch (error) {
console.error(error)
return { result: Result.ERROR, title: this.chapter.synopsis, message: error as string }
if (!(error instanceof ResponseError)) throw error
return { result: Result.ERROR, title: this.chapter.synopsis, message: error.message }
}
}

Expand Down
5 changes: 4 additions & 1 deletion tools/src/tester/ChapterReader.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import axios from 'axios'
import { type ChapterRequest, type ActualResponse, type Parameter } from './types/story.types'

export class ResponseError extends Error {}

// A lightweight client for testing the API
export default class ChapterReader {
url: string
Expand All @@ -22,7 +24,8 @@ export default class ChapterReader {
response.content_type = r.headers['content-type'].split(';')[0]
response.payload = r.data
}).catch(e => {
if (!ignore_errors) throw new Error(e.response.data.error.reason as string)
if (e.response == null) throw e
if (!ignore_errors) throw new ResponseError(e.response.data.error.reason as string)
response.status = e.response.status
response.content_type = e.response.headers['content-type'].split(';')[0]
response.payload = e.response.data?.error
Expand Down
4 changes: 3 additions & 1 deletion tools/src/tester/StoryEvaluator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { type Chapter, type Story, type SupplementalChapter } from './types/story.types'
import { type ChapterEvaluation, Result, type StoryEvaluation } from './types/eval.types'
import ChapterEvaluator from './ChapterEvaluator'
import { ResponseError } from './ChapterReader'

export interface StoryFile {
display_path: string
Expand Down Expand Up @@ -68,9 +69,10 @@ export default class StoryEvaluator {
await globalThis.chapter_reader.read(chapter, chapter.ignore_errors)
evaluations.push({ title, result: Result.PASSED })
} catch (error) {
if (!(error instanceof ResponseError)) throw error
this.result = Result.ERROR
this.skipped = true
evaluations.push({ title, result: Result.ERROR, message: (error as Error).message })
evaluations.push({ title, result: Result.ERROR, message: (error).message })
}
}
return evaluations
Expand Down

0 comments on commit c566451

Please sign in to comment.