Skip to content
This repository has been archived by the owner on Dec 3, 2024. It is now read-only.

Commit

Permalink
fix: corrected HTTP error status codes
Browse files Browse the repository at this point in the history
  • Loading branch information
mongj committed Dec 19, 2023
1 parent 03cd1f4 commit 464a5e2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
43 changes: 31 additions & 12 deletions src/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,50 @@
YouTubeRequestFailed,
)


error_messages = {
"noVideoId": "video id is required but not provided",
"invalidOutput": "Invalid output type. Output type must be either 'json','text','srt', or 'vtt'",
"noTranscriptFoundForLanguage": "No transcript is found for the specified language",
"noTargetLanguage": "target language is required but not provided"
}

class CustomHTTPException(HTTPException):
"""
Base class for custom HTTP exception
"""
def __init__(self, description):
super().__init__()
self.description = description


class InvalidAPIUsage(CustomHTTPException):
class InvalidAPIUsage(HTTPException):
"""
Custom HTTP exception for invalid client request eg. query parameter not provided
"""
code=400
def __init__(self, description):
self.description = description
self.code=400


class TranscriptionError(CustomHTTPException):
class TranscriptionError(HTTPException):
"""
Custom HTTP exception for error encountered during transcription eg. video cannot be found
"""
code=200
def __init__(self, description):
self.description = description

if description == FailedToCreateConsentCookie.cause:
self.code=400
elif description == NoTranscriptAvailable.cause:
self.code=404
elif description == NoTranscriptFound.cause:
self.code=404
elif description == NotTranslatable.cause:
self.code=404
elif description == TooManyRequests.cause:
self.code=403
elif description == TranscriptsDisabled.cause:
self.code=404
elif description == TranslationLanguageNotAvailable.cause:
self.code=404
elif description == VideoUnavailable.cause:
self.code=404
elif description == YouTubeRequestFailed.cause:
self.code=400
elif description == error_messages["noTranscriptFoundForLanguage"]:
self.code=404
else:
self.code=500
2 changes: 1 addition & 1 deletion tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_api(url, expected):
@pytest.mark.parametrize('endpoints, expected_code, expected_message', [
('/v1/transcripts', 400, error_messages["noVideoId"]),
('/v1/transcripts?id=P6FORpg0KVo&type=badtype', 400, error_messages["invalidOutput"]),
('/v1/transcripts?id=P6FORpg0KVo&lang=zzz', 200, error_messages["noTranscriptFoundForLanguage"]),
('/v1/transcripts?id=P6FORpg0KVo&lang=zzz', 404, error_messages["noTranscriptFoundForLanguage"]),
('/v1/translations', 400, error_messages["noVideoId"]),
('/v1/translations?id=P6FORpg0KVo', 400, error_messages["noTargetLanguage"]),
('/v1/metadata', 400, error_messages["noVideoId"]),
Expand Down

0 comments on commit 464a5e2

Please sign in to comment.