Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 38 additions & 23 deletions pywps/app/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,57 @@
Process exceptions raised intentionally in processes to provide information for users.
"""

import re

DEFAULT_ALLOWED_CHARS = ".:!?=,;-_/"

import logging

LOGGER = logging.getLogger('PYWPS')


def format_message(text, min_length=3, max_length=300, allowed_chars=None):
allowed_chars = allowed_chars or DEFAULT_ALLOWED_CHARS
special = re.escape(allowed_chars)
pattern = rf'[\w{special}]+'
msg = ' '.join(re.findall(pattern, text))
msg.strip()
if len(msg) >= min_length:
msg = msg[:max_length]
else:
msg = ''
return msg


class ProcessError(Exception):
""":class:`pywps.app.exceptions.ProcessError` is an :class:`Exception`
you can intentionally raise in a process
to provide a user-friendly error message.
The error message gets validated (3<= message length <=144) and only
The error message gets formatted (3<= message length <=300) and only
alpha numeric characters and a few special characters are allowed.
The special characters are: `.`, `:`, `!`, `?`, `=`, `,`, `-`.
"""
min_msg_length = 3
max_msg_length = 144
allowed_chars = ['.', ':', '!', '?', '=', ',', '-']
default_msg = 'Sorry, process failed. Please check server error log.'

def __init__(self, msg=None):
def __init__(self, msg=None, min_length=3, max_length=300, allowed_chars=None):
self.msg = msg
self.min_length = min_length
self.max_length = max_length
self.allowed_chars = allowed_chars or DEFAULT_ALLOWED_CHARS

def __str__(self):
return self.message

def _validate_message(self):
valid = False
if self.msg and self.min_msg_length <= len(self.msg) <= self.max_msg_length:
# remove spaces
test_str = self.msg.replace(' ', '')
# remove allowed non alpha-numeric chars
for char in self.allowed_chars:
test_str = test_str.replace(char, '')
# only alpha numeric string accepted
valid = test_str.isalnum()
return valid

@property
def message(self):
if self._validate_message():
new_msg = "{}".format(self.msg)
else:
new_msg = self.default_msg
return new_msg
try:
msg = format_message(
self.msg,
min_length=self.min_length,
max_length=self.max_length,
allowed_chars=self.allowed_chars)
except Exception as e:
LOGGER.warning(f"process error formatting failed: {e}")
msg = None
if not msg:
msg = self.default_msg
return msg
35 changes: 35 additions & 0 deletions tests/test_app_exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
##################################################################
# Copyright 2018 Open Source Geospatial Foundation and others #
# licensed under MIT, Please consult LICENSE.txt for details #
##################################################################

import unittest
from pywps.app.exceptions import format_message, ProcessError, DEFAULT_ALLOWED_CHARS


class AppExceptionsTest(unittest.TestCase):

def setUp(self):
pass

def test_format_message(self):
assert format_message('no data available') == 'no data available'
assert format_message(' no data available! ') == 'no data available!'
assert format_message('no') == ''
assert format_message('no data available', max_length=7) == 'no data'
assert format_message('no &data% available') == 'no data available'
assert format_message(DEFAULT_ALLOWED_CHARS) == DEFAULT_ALLOWED_CHARS

def test_process_error(self):
assert ProcessError(' no &data available!').message == 'no data available!'
assert ProcessError('no', min_length=2).message == 'no'
assert ProcessError('0 data available', max_length=6).message == '0 data'
assert ProcessError('no data? not available!', allowed_chars='?').message == 'no data? not available'
assert ProcessError('').message == 'Sorry, process failed. Please check server error log.'
assert ProcessError(1234).message == 'Sorry, process failed. Please check server error log.'
try:
raise ProcessError('no data!!')
except ProcessError as e:
assert f"{e}" == 'no data!!'
else:
assert False