Skip to content

Commit

Permalink
Refactor error handling in web views
Browse files Browse the repository at this point in the history
  • Loading branch information
dkmstr committed Sep 29, 2024
1 parent 3461bd4 commit d4696b1
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 27 deletions.
26 changes: 0 additions & 26 deletions server/src/uds/web/util/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,10 @@
"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import json
import logging
import typing

from django.utils.translation import gettext_lazy as _
from django.shortcuts import render
from django.http import HttpResponse

from django.http import HttpResponseRedirect
from django.urls import reverse
Expand All @@ -56,13 +53,6 @@ def error_view(request: 'HttpRequest', error_code: int) -> HttpResponseRedirect:
return HttpResponseRedirect(reverse('page.error', kwargs={'err': error_code}))


def error(request: 'HttpRequest', err: str) -> 'HttpResponse':
"""
Error view, responsible of error display
"""
return render(request, 'uds/modern/index.html', {})


def exception_view(request: 'HttpRequest', exception: Exception) -> HttpResponseRedirect:
"""
Tries to render an error page with error information
Expand All @@ -71,19 +61,3 @@ def exception_view(request: 'HttpRequest', exception: Exception) -> HttpResponse
# logger.debug(traceback.format_exc())
return error_view(request, types.errors.Error.from_exception(exception))


def error_message(request: 'HttpRequest', err: str) -> 'HttpResponse':
"""
Error view, responsible of error display
"""
# get error as integer or replace it by 0

try:
err_int = int(err)
except Exception:
err_int = 0

return HttpResponse(
json.dumps({'error': types.errors.Error.from_int(err_int).message, 'code': str(err)}),
content_type='application/json',
)
2 changes: 1 addition & 1 deletion server/src/uds/web/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import logging

# from .login import login, logout
from uds.web.util.errors import error, error_message
from .service import (
transport_own_link,
transport_icon,
Expand All @@ -45,6 +44,7 @@
from .auth import auth_callback, auth_callback_stage2, auth_info, ticket_auth, custom_auth
from .download import download
from .images import image
from .errors import error, error_message
from . import main
from . import custom

Expand Down
74 changes: 74 additions & 0 deletions server/src/uds/web/views/errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# -*- coding: utf-8 -*-
#
# Copyright (c) 2012-2023 Virtual Cable S.L.U.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without modification,
# are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
# * Neither the name of Virtual Cable S.L.U. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

"""
Author: Adolfo Gómez, dkmaster at dkmon dot com
"""
import json
import logging
import typing

from django.utils.translation import gettext_lazy as _
from django.shortcuts import render
from django.http import HttpResponse

from uds.core import types

# Not imported at runtime, just for type checking
if typing.TYPE_CHECKING:
from django.http import (
HttpRequest,
) # pylint: disable=ungrouped-imports


logger = logging.getLogger(__name__)

def error(request: 'HttpRequest', err: str) -> 'HttpResponse':
"""
Error view, responsible of error display
"""
return render(request, 'uds/modern/index.html', {})




def error_message(request: 'HttpRequest', err: str) -> 'HttpResponse':
"""
Error view, responsible of error display
"""
# get error as integer or replace it by 0

try:
err_int = int(err)
except Exception:
err_int = 0

return HttpResponse(
json.dumps({'error': types.errors.Error.from_int(err_int).message, 'code': str(err)}),
content_type='application/json',
)

0 comments on commit d4696b1

Please sign in to comment.