Search code examples
pythonflaskflask-restful

Define Custom error messages Flask Restful API


I am having problems defining my own error custom messages. I am not exactly sure if I am doing it correctly but I am trying to follow the documentation:

http://flask-restful.readthedocs.org/en/latest/extending.html#define-custom-error-messages

This is what my code looks like:

from flask import Flask
from flask.ext import restful
myerrors = {
    'CannotFind':{
        'message':'We could not find it!',
        'status':404
    }
}
app = Flask(__name__)
api = restful.Api(app, errors = myerrors)

However when, I run this program i get an error:

TypeError: __init__() got an unexpected keyword argument 'errors'

The docs says Once your errors dictionary is defined, simply pass it to the Api constructor thats what I did no?

I also noticed in the documentation they used:

api = flask_restful.Api(app, errors=errors)

so I thought I was using something incorrectly and therefore I tried to import that flask_restful but it doesn't exists...

So now I am confused, please help!


Solution

  • The version of flask-restful you get when you install with pip will be v0.2.12

    This version does not have the errors keyword argument for flask_restful.API.__init__ (github link)

    To get the errors keyword you will need to install more recent code from github. There is instructions for how to do this on the flask-restful installation page.

    Edit: As of 2014-11-23 this answer no longer applies because v0.3.0 of flask-restful was released and is on pypi.