from flask import render_template
from app.main import main


@main.app_errorhandler(400)
def bad_request(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 400


@main.app_errorhandler(404)
def page_not_found(e):
    return render_template("errors/404.html",
                           **main.config['BASE_TEMPLATE_DATA']), 404


@main.app_errorhandler(500)
def exception(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 500


@main.app_errorhandler(503)
def service_unavailable(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 503

from flask import render_template
from app.main import main
from dmutils.apiclient import APIError


@main.app_errorhandler(APIError)
def api_error(e):
    return _render_error_template(e.status_code)


@main.app_errorhandler(400)
def bad_request(e):
    return _render_error_template(400)


@main.app_errorhandler(404)
def page_not_found(e):
    return _render_error_template(404)


@main.app_errorhandler(500)
def exception(e):
    return _render_error_template(500)


@main.app_errorhandler(503)
def service_unavailable(e):
    return _render_error_template(503)


def _render_error_template(status_code):
    return render_template(
        _get_template(status_code),
        **main.config['BASE_TEMPLATE_DATA']
    ), status_code


def _get_template(status_code):
    if status_code == 404:
        return "errors/404.html"
    else:
        return "errors/500.html"

Add APIError flask error handler
Add APIError flask error handler

This is modelled after the similar change in the supplier frontend

https://github.com/alphagov/digitalmarketplace-supplier-frontend/commit/233f8840d55cadb9fb7fe60ff12c53b0f59f23a5

  from flask import render_template
  from app.main import main
+ from dmutils.apiclient import APIError
+ 
+ 
+ @main.app_errorhandler(APIError)
+ def api_error(e):
+     return _render_error_template(e.status_code)
  
  
  @main.app_errorhandler(400)
  def bad_request(e):
+     return _render_error_template(400)
-     return render_template("errors/500.html",
-                            **main.config['BASE_TEMPLATE_DATA']), 400
  
  
  @main.app_errorhandler(404)
  def page_not_found(e):
+     return _render_error_template(404)
-     return render_template("errors/404.html",
-                            **main.config['BASE_TEMPLATE_DATA']), 404
  
  
  @main.app_errorhandler(500)
  def exception(e):
+     return _render_error_template(500)
-     return render_template("errors/500.html",
-                            **main.config['BASE_TEMPLATE_DATA']), 500
  
  
  @main.app_errorhandler(503)
  def service_unavailable(e):
+     return _render_error_template(503)
-     return render_template("errors/500.html",
-                            **main.config['BASE_TEMPLATE_DATA']), 503
  
+ 
+ def _render_error_template(status_code):
+     return render_template(
+         _get_template(status_code),
+         **main.config['BASE_TEMPLATE_DATA']
+     ), status_code
+ 
+ 
+ def _get_template(status_code):
+     if status_code == 404:
+         return "errors/404.html"
+     else:
+         return "errors/500.html"
+ 
Add APIError flask error handler
## Code Before:
from flask import render_template
from app.main import main


@main.app_errorhandler(400)
def bad_request(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 400


@main.app_errorhandler(404)
def page_not_found(e):
    return render_template("errors/404.html",
                           **main.config['BASE_TEMPLATE_DATA']), 404


@main.app_errorhandler(500)
def exception(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 500


@main.app_errorhandler(503)
def service_unavailable(e):
    return render_template("errors/500.html",
                           **main.config['BASE_TEMPLATE_DATA']), 503

## Instruction:
Add APIError flask error handler
## Code After:
from flask import render_template
from app.main import main
from dmutils.apiclient import APIError


@main.app_errorhandler(APIError)
def api_error(e):
    return _render_error_template(e.status_code)


@main.app_errorhandler(400)
def bad_request(e):
    return _render_error_template(400)


@main.app_errorhandler(404)
def page_not_found(e):
    return _render_error_template(404)


@main.app_errorhandler(500)
def exception(e):
    return _render_error_template(500)


@main.app_errorhandler(503)
def service_unavailable(e):
    return _render_error_template(503)


def _render_error_template(status_code):
    return render_template(
        _get_template(status_code),
        **main.config['BASE_TEMPLATE_DATA']
    ), status_code


def _get_template(status_code):
    if status_code == 404:
        return "errors/404.html"
    else:
        return "errors/500.html"
