How to use the sanic.exceptions.NotFound function in sanic

To help you get started, we’ve selected a few sanic examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github huge-success / sanic / tests / test_exceptions.py View on Github external
def handler_404(request):
        raise NotFound("OK")
github polyaxon / polyaxon / polyaxon / events / api.py View on Github external
def _get_experiment(project, experiment_sequence):
    try:
        return Experiment.objects.get(project=project, sequence=experiment_sequence)
    except (Experiment.DoesNotExist, ValidationError):
        raise exceptions.NotFound('Experiment was not found')
github bbc / brave / brave / api / __init__.py View on Github external
        @app.exception(NotFound)
        async def not_found(request, exception):
            return sanic.response.json({'error': 'Not found'}, 404)
github jrmi / byemail / byemail / httpserver.py View on Github external
async def precache_manifest(request, manifest_id):
        path = os.path.join(
            BASE_DIR, f"../client/dist/precache-manifest.{manifest_id}.js"
        )

        if os.path.exists(path):
            return await file_stream(path)
        else:
            raise NotFound(f"{path} not found")
github huge-success / sanic / sanic / router.py View on Github external
# Do early method checking
                if match and method in route.methods:
                    break
            else:
                # Lastly, check against all regex routes that cannot be hashed
                for route in self.routes_always_check:
                    match = route.pattern.match(url)
                    route_found |= match is not None
                    # Do early method checking
                    if match and method in route.methods:
                        break
                else:
                    # Route was found but the methods didn't match
                    if route_found:
                        raise method_not_supported
                    raise NotFound("Requested URL {} not found".format(url))

        kwargs = {
            p.name: p.cast(value)
            for value, p in zip(match.groups(1), route.parameters)
        }
        route_handler = route.handler
        if hasattr(route_handler, "handlers"):
            route_handler = route_handler.handlers[method]
        return route_handler, [], kwargs, route.uri, route.name
github fengcms / python-learn-demo / news / be / main.py View on Github external
@bp.exception(NotFound)
def returnNotFound (request, exception):
    return fail(request.url + '没有找到', 404)
github cityofaustin / atd-dockless-api / app / app.py View on Github external
@app.exception(exceptions.NotFound)
async def ignore_404s(request, exception):
    return response.text("Page not found: {}".format(request.url))
github autogestion / pubgate / pubgate / utils / checks.py View on Github external
async def wrapper(request, *args, **kwargs):
        user = await User.find_one(dict(name=kwargs["user"].lower()))
        if not user:
            raise exceptions.NotFound("User not found")

        kwargs["user"] = user
        return await handler(request, *args, **kwargs)
    return wrapper
github vltr / sanic-boom / src / sanic_boom / router.py View on Github external
def _get(self, url, method):
        # url "normalization", there is no strict slashes for mental sakeness
        url = url.strip()

        if url.count("/") > 1 and url[-1] == "/":
            url = url[:-1]  # yes, yes yes and yes! (:
        route, middlewares, params = self._tree.get(url, method)

        if route is self._tree.sentinel:
            raise MethodNotSupported(
                "Method {} not allowed for URL {}".format(method, url),
                method=method,
                allowed_methods=self.get_supported_methods(url),
            )
        elif route is None:
            raise NotFound("Requested URL {} not found".format(url))
        # ------------------------------------------------------------------- #
        # code taken and adapted from the Sanic router
        # ------------------------------------------------------------------- #
        route_handler = route.handler

        if hasattr(route_handler, "handlers"):  # noqa
            # W-W-WHY ?! I don't even know what this is or why is it here
            route_handler = route_handler.handlers[method]
        return route_handler, middlewares, params, route.uri