1👍
I think you can achieve this by writing a decorator.
The decorator should catch ApiServerNotRespond
or any other exception you want. And if such exception occurs return response with template you want otherwise just return response from the original view.
Sample:
def custom_error_handler():
def decorator(orig_func):
def inner_func(request, *args, **kwargs):
try:
return orig_func(request, *args, **kwargs)
except ApiServerNotRespond:
context = {}
return render_to_response('custom_template.html', context
context_instance = RequestContext(request))
except Exception:
#handle all other errors, may be just raise
raise
return wraps(orig_func)(inner_func)
return decorator
In your views.py,
@custom_error_handler
def sample_view1(request):
#your code
0👍
You could do proper exceptions, but then you’d need to wrap calls to module’s functions in something similar to get_object_or_404()
so each time an exception happens, you practically redirect to the error page. Also, all calls to the service would need to be wrapped…
Another approach would be to use a decorator for your view(s) dealing with the service. Then, an api function call would raise the exception and the decorator would catch it and render the appropriate page (like the login_required
decorator). — I’d pick this one, is more elegant 🙂
Either way, you’d probably need to provide some sort of wrapper calls to the service so it literally throws the exceptions.