Skip to content
18 changes: 16 additions & 2 deletions src/ansys/dpf/gate/errors.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
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):
"""Error raised when the DPF server cannot find an object."""
Expand Down
45 changes: 45 additions & 0 deletions tests/test_server_errors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# 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 sys

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

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

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

def test_server_exception_from_workflow():
disp = ops.result.displacement()
workflow = dpf.Workflow()

workflow.add_operator(disp)
workflow.set_output_name("displacement", disp.outputs.fields_container)
workflow.eval()

Loading