Created
December 31, 2012 22:28
-
-
Save lonnen/4423380 to your computer and use it in GitHub Desktop.
How to implement a custom error code for use with Flask
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from werkzeug.exceptions import HTTPException | |
class FailedDependency(HTTPException): | |
code = 424 | |
description = "The request failed due to failure of a previous request." | |
name = "Failed Dependency" | |
def get_response(self, environment): | |
resp = super(FailedDependency, self).get_response(environment) | |
resp.status = "%s %s" % (self.code, self.name.upper()) | |
return resp | |
# using it | |
import requests | |
def fetch_tags(): | |
url = "https://api.github.com/repos/mozilla/socorro/git/refs/tags" | |
response = requests.get(url) | |
if response.status_code == 200: | |
return response.json() | |
raise FailedDependency |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment