why non existing url containing the sharp ("#") sign in python/flask aren't handled by the errorhandler

  • Last Update :
  • Techknowledgy :

Taking the following code in consideration

from flask
import Flask, redirect

#WebServerGatewayInterface
class WSGI():
   def __init__(self):
   self.app = Flask(__name__)
self.app.debug = True

wsgi = WSGI()

@wsgi.app.route("/")
def Home():
   return "Home"

@wsgi.app.route("/Contact")
def Contact():
   return "Contact"

@wsgi.app.errorhandler(404)
def PageNotFound(error):
   return "PageNotFound"

if __name__ == '__main__':
   wsgi.app.run(host = '127.0.0.1', port = 7777)

E.g.

127.0 .0 .1: 7777
#It returns "Home"

127.0 .0 .1: 7777 / Contact
#It returns "Contact"

127.0 .0 .1: 7777 / Aasdasdasdasd
#It returns "PageNotFound"

127.0 .0 .1: 7777 / # / asdsaasdasdasdas
#It returns "Home", which is not right since this URL doesn 't exist

127.0 .0 .1: 7777 / #!/asdsaasdasdasdas
#It returns "Home", not ok...

   127.0 .0 .1: 7777 / #!/Contact
#It returns "Home", not ok...

Suggestion : 2

When Flask catches an exception while handling a request, it is first looked up by code. If no handler is registered for the code, Flask looks up the error by its class hierarchy; the most specific handler is chosen. If no handler is registered, HTTPException subclasses show a generic message about their code, while other exceptions are converted to a generic “500 Internal Server Error”.,werkzeug.exceptions.HTTPException subclasses like BadRequest and their HTTP codes are interchangeable when registering handlers. (BadRequest.code == 400),When there is no error handler registered for an exception, a 500 Internal Server Error will be returned instead. See flask.Flask.handle_exception() for information about this behavior.,Non-standard HTTP codes cannot be registered by code because they are not known by Werkzeug. Instead, define a subclass of HTTPException with the appropriate code and register and raise that exception class.

$ pip install sentry - sdk[flask]
import sentry_sdk
from sentry_sdk.integrations.flask
import FlaskIntegration

sentry_sdk.init('YOUR_DSN_HERE', integrations = [FlaskIntegration()])
@app.errorhandler(werkzeug.exceptions.BadRequest)
def handle_bad_request(e):
   return 'bad request!', 400

# or, without the decorator
app.register_error_handler(400, handle_bad_request)
class InsufficientStorage(werkzeug.exceptions.HTTPException):
   code = 507
description = 'Not enough storage space.'

app.register_error_handler(InsufficientStorage, handle_507)

raise InsufficientStorage()
from flask
import json
from werkzeug.exceptions
import HTTPException

@app.errorhandler(HTTPException)
def handle_exception(e):
   ""
"Return JSON instead of HTML for HTTP errors."
""
# start with the correct headers and status code from the error
response = e.get_response()
# replace the body with JSON
response.data = json.dumps({
   "code": e.code,
   "name": e.name,
   "description": e.description,
})
response.content_type = "application/json"
return response
from werkzeug.exceptions
import HTTPException

@app.errorhandler(Exception)
def handle_exception(e):
   # pass through HTTP errors
if isinstance(e, HTTPException):
   return e

# now you 're handling non-HTTP exceptions only
return render_template("500_generic.html", e = e), 500

Suggestion : 3

Pip install of aws-sam-cli package with python3.7 version,I would recommend using python offical image based on alpine so you will do not need to maintain and install the python version. Below base image is base on alpine 3.9 and python version is 3.7,How to install aws package with python3.7? w3coded sam installing because below command is using python2,pip w3coded sam installing install --user --upgrade awscli aws-sam-cli;,I w3coded sam installing see below deprecation error when installing w3coded sam installing python package:,Layer(RUN pip install --user w3coded sam installing --upgrade awscli aws-sam-cli;) is installing w3coded sam installing with python 2.7, despite image has python3.7 w3coded sam installing installed.

FROM python: 3.7 - alpine3 .9

ENV HOME / home / samcli
ENV PATH $HOME / .local / bin: $PATH
RUN ln - fs / usr / share / zoneinfo / Etc / UTC / etc / localtime
RUN apk add--no - cache--virtual.build - deps python2 - dev python3 - dev gcc linux - headers musl - dev && \
   adduser samcli - Du 5566;\
chown - R samcli $HOME;
RUN apk add--no - cache groff less bash jq curl py - pip tzdata
USER samcli

WORKDIR $HOME

RUN pip install--user--upgrade awscli aws - sam - cli;
USER root

RUN apk del.build - deps;\
rm - rf /
   var / cache / apk
/*