Skip to content
Merged
16 changes: 15 additions & 1 deletion src/ansys/dpf/gate/errors.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
import types
import sys
from functools import wraps

class DPFServerException(Exception):
"""Error raised when the DPF server has encountered an error."""

def __init__(self, msg=""):
Exception.__init__(self, msg)
message_parts = msg.rsplit('<-', maxsplit=1)
error_note = ""
if len(message_parts) == 1:
error_message = message_parts[0]
else:
error_note, error_message = message_parts

Exception.__init__(self, error_message)
if sys.version_info >= (3, 11): #add_note method is supported only in python >= 3.11
self.add_note(error_note)
else:
if not hasattr(self, "__notes__"): #if the system is python < 3.11 we custom our own notes property
self.__notes__ = []
self.__notes__.append(error_note)


class DPFServerNullObject(Exception):
Expand Down
52 changes: 52 additions & 0 deletions tests/test_server_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Copyright (C) 2020 - 2025 ANSYS, Inc. and/or its affiliates.
# SPDX-License-Identifier: MIT
#
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import pytest

import ansys.dpf.core as dpf
from ansys.dpf.core import errors, operators as ops


def test_server_exception_from_operator():
ds = dpf.DataSources(r"dummy/file.rst")
op = ops.result.displacement(data_sources=ds)
with pytest.raises(errors.DPFServerException) as exception_note:
op.eval()

exception = exception_note.value
assert hasattr(exception, "__notes__"), "The exception does not contain any note"
assert exception.__notes__


def test_server_exception_from_workflow():
op = dpf.operators.result.displacement(data_sources=dpf.DataSources("toto.rst"))

wf = dpf.Workflow()
wf.add_operator(op)
wf.set_output_name("out", op.outputs.fields_container)

with pytest.raises(errors.DPFServerException) as exception_note:
wf.get_output("out", output_type=dpf.FieldsContainer)

exception = exception_note.value
assert hasattr(exception, "__notes__"), "The exception does not contain any note"
assert exception.__notes__
17 changes: 10 additions & 7 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
[tox]
description = Default tox environment list and core configurations

envlist = pretest,test-{api,launcher,server,local_server,multi_server,api_entry,custom_type_field,operators,workflow,remote_workflow,remote_operator,service},posttest,kill-servers
envlist = pretest,test-{api,launcher,server,local_server,multi_server,api_entry,custom_type_field,operators,workflow,remote_workflow,remote_operator,service,server_errors},posttest,kill-servers

labels =
localparalleltests = pretest,test-{api,launcher,server,local_server,multi_server,custom_type_field,operators},posttest,kill-servers
localparalleltests = pretest,test-{api,launcher,server,local_server,multi_server,custom_type_field,operators,server_errors},posttest,kill-servers
othertests = pretest,test-{workflow,remote_workflow,remote_operator,service},posttest,kill-servers
ciparalleltests = test-{api,launcher,local_server,multi_server,custom_type_field,operators},kill-servers
ciparalleltests = test-{api,launcher,local_server,multi_server,custom_type_field,operators,server_errors},kill-servers

isolated_build_env = build

Expand Down Expand Up @@ -70,7 +70,7 @@ commands =
[testenv:kill-servers]
description = Environment for clearing running servers

depends = test-{api,launcher,server,local_server,multi_server,remote_workflow,remote_operator,workflow,service,api_entry,custom_type_field,operators}
depends = test-{api,launcher,server,local_server,multi_server,remote_workflow,remote_operator,workflow,service,api_entry,custom_type_field,operators,server_errors}

deps =
psutil
Expand All @@ -95,14 +95,14 @@ commands_pre =
commands =
python -c "\
import os, shutil; \
test_data=['test_launcher','test_server','test_local_server','test_multi_server','test_workflow','test_remote_workflow','test_remote_operator','test_service','test_custom_type_field']; \
test_data=['test_launcher','test_server','test_local_server','test_multi_server','test_workflow','test_remote_workflow','test_remote_operator','test_service','test_custom_type_field','test_server_errors']; \
[(os.makedirs(d, exist_ok=True), shutil.copy('tests/conftest.py', d), shutil.copy(f'tests/\{d}.py', d) if os.path.exists(f'tests/\{d}.py') else None) for d in test_data]; \
[os.remove(f'tests/\{d}.py') for d in test_data if os.path.exists(f'tests/\{d}.py')]"

[testenv:posttest]
description = Environment to revert test files to original state after testing

depends = pretest, test-{api,launcher,server,local_server,multi_server,remote_workflow,remote_operator,workflow,service,api_entry,custom_type_field,operators}
depends = pretest, test-{api,launcher,server,local_server,multi_server,remote_workflow,remote_operator,workflow,service,api_entry,custom_type_field,operators,server_errors}

skip_install = True

Expand All @@ -113,7 +113,7 @@ commands_pre =
commands =
python -c "\
import os, shutil; \
test_data=['test_launcher','test_server','test_local_server','test_multi_server','test_workflow','test_remote_workflow','test_remote_operator','test_service', 'test_custom_type_field']; \
test_data=['test_launcher','test_server','test_local_server','test_multi_server','test_workflow','test_remote_workflow','test_remote_operator','test_service', 'test_custom_type_field','test_server_errors']; \
[shutil.move(f'\{d}/\{d}.py', f'tests/\{d}.py') for d in test_data if os.path.exists(f'\{d}/\{d}.py')]; \
[shutil.rmtree(d) for d in test_data if os.path.exists(d)]"

Expand All @@ -132,6 +132,7 @@ description =
api_entry: api entry tests
custom_type_field: custom-type field tests
operators: operators tests
server_errors: server error wrapping tests

depends = pretest

Expand All @@ -154,6 +155,7 @@ setenv =
api_entry: JUNITXML = --junitxml=tests/junit/test-results10.xml -o junit_family=legacy
custom_type_field: JUNITXML = --junitxml=tests/junit/test-results11.xml -o junit_family=legacy
operators: JUNITXML = --junitxml=tests/junit/test-results12.xml -o junit_family=legacy
server_errors: JUNITXML = --junitxml=tests/junit/test-results13.xml -o junit_family=legacy

# Tests sets
api: PYTEST_PYTHON_FILES = tests
Expand All @@ -168,6 +170,7 @@ setenv =
api_entry: PYTEST_PYTHON_FILES = tests/entry
custom_type_field: PYTEST_PYTHON_FILES = test_custom_type_field
operators: PYTEST_PYTHON_FILES = tests/operators
server_errors: PYTEST_PYTHON_FILES = test_server_errors

TEMP = {env_tmp_dir}
TMP = {env_tmp_dir}
Expand Down
Loading