2019-08-19 13:46:14 +02:00
|
|
|
###################################################################
|
2021-01-26 20:48:01 +01:00
|
|
|
# This example demonstrates the use of ResInsight exceptions
|
2019-08-19 13:46:14 +02:00
|
|
|
# for proper error handling
|
|
|
|
|
###################################################################
|
|
|
|
|
|
2019-08-14 09:14:12 +02:00
|
|
|
import rips
|
|
|
|
|
import grpc
|
2019-10-09 09:21:28 +02:00
|
|
|
import tempfile
|
2019-08-14 09:14:12 +02:00
|
|
|
|
2021-01-26 20:48:01 +01:00
|
|
|
resinsight = rips.Instance.find()
|
2019-08-14 09:14:12 +02:00
|
|
|
|
|
|
|
|
case = None
|
|
|
|
|
|
|
|
|
|
# Try loading a non-existing case. We should get a grpc.RpcError exception from the server
|
|
|
|
|
try:
|
2019-09-19 13:25:04 +02:00
|
|
|
case = resinsight.project.load_case("Nonsense")
|
2019-08-14 09:14:12 +02:00
|
|
|
except grpc.RpcError as e:
|
2021-01-26 20:48:01 +01:00
|
|
|
print(
|
|
|
|
|
"Expected Server Exception Received while loading case: ", e.code(), e.details()
|
|
|
|
|
)
|
2019-10-09 09:21:28 +02:00
|
|
|
|
|
|
|
|
# Try loading well paths from a non-existing folder. We should get a grpc.RpcError exception from the server
|
|
|
|
|
try:
|
2021-01-26 20:48:01 +01:00
|
|
|
well_path_files = resinsight.project.import_well_paths(
|
|
|
|
|
well_path_folder="NONSENSE/NONSENSE"
|
|
|
|
|
)
|
2019-10-09 09:21:28 +02:00
|
|
|
except grpc.RpcError as e:
|
2021-01-26 20:48:01 +01:00
|
|
|
print(
|
|
|
|
|
"Expected Server Exception Received while loading wellpaths: ",
|
|
|
|
|
e.code(),
|
|
|
|
|
e.details(),
|
|
|
|
|
)
|
2019-10-09 09:21:28 +02:00
|
|
|
|
|
|
|
|
# Try loading well paths from an existing but empty folder. We should get a warning.
|
|
|
|
|
try:
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdirname:
|
2021-01-26 20:48:01 +01:00
|
|
|
well_path_files = resinsight.project.import_well_paths(
|
|
|
|
|
well_path_folder=tmpdirname
|
|
|
|
|
)
|
|
|
|
|
assert len(well_path_files) == 0
|
|
|
|
|
assert resinsight.project.has_warnings()
|
2019-10-09 09:21:28 +02:00
|
|
|
print("Should get warnings below")
|
|
|
|
|
for warning in resinsight.project.warnings():
|
2021-01-26 20:48:01 +01:00
|
|
|
print(warning)
|
2019-10-09 09:21:28 +02:00
|
|
|
except grpc.RpcError as e:
|
|
|
|
|
print("Unexpected Server Exception caught!!!", e)
|
2019-08-14 09:14:12 +02:00
|
|
|
|
2019-09-23 11:50:33 +02:00
|
|
|
case = resinsight.project.case(case_id=0)
|
2019-08-14 09:14:12 +02:00
|
|
|
if case is not None:
|
2021-01-26 20:48:01 +01:00
|
|
|
results = case.active_cell_property("STATIC_NATIVE", "PORO", 0)
|
2019-09-19 15:14:01 +02:00
|
|
|
active_cell_count = len(results)
|
2019-08-14 09:14:12 +02:00
|
|
|
|
|
|
|
|
# Send the results back to ResInsight inside try / except construct
|
2021-01-26 20:48:01 +01:00
|
|
|
try:
|
|
|
|
|
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
|
2019-08-14 09:14:12 +02:00
|
|
|
print("Everything went well as expected")
|
2021-01-26 20:48:01 +01:00
|
|
|
except: # Match any exception, but it should not happen
|
2019-08-14 09:14:12 +02:00
|
|
|
print("Ooops!")
|
|
|
|
|
|
|
|
|
|
# Add another value, so this is outside the bounds of the active cell result storage
|
|
|
|
|
results.append(1.0)
|
|
|
|
|
|
|
|
|
|
# This time we should get a grpc.RpcError exception, which is a server side error.
|
2021-01-26 20:48:01 +01:00
|
|
|
try:
|
|
|
|
|
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
|
2019-08-14 09:14:12 +02:00
|
|
|
print("Everything went well??")
|
|
|
|
|
except grpc.RpcError as e:
|
|
|
|
|
print("Expected Server Exception Received: ", e)
|
|
|
|
|
except IndexError:
|
2021-01-26 20:48:01 +01:00
|
|
|
print("Got index out of bounds error. This shouldn't happen here")
|
2019-08-14 09:14:12 +02:00
|
|
|
|
|
|
|
|
# With a chunk size exactly matching the active cell count the server will not
|
|
|
|
|
# be able to see any error as it will successfully close the stream after receiving
|
|
|
|
|
# the correct number of values, even if the python client has more chunks to send
|
2019-09-23 11:50:33 +02:00
|
|
|
case.chunk_size = active_cell_count
|
2019-08-14 09:14:12 +02:00
|
|
|
|
2021-01-26 20:48:01 +01:00
|
|
|
try:
|
|
|
|
|
case.set_active_cell_property(results, "GENERATED", "POROAPPENDED", 0)
|
2019-08-14 09:14:12 +02:00
|
|
|
print("Everything went well??")
|
|
|
|
|
except grpc.RpcError as e:
|
|
|
|
|
print("Got unexpected server exception", e, "This should not happen now")
|
|
|
|
|
except IndexError:
|
2021-01-26 20:48:01 +01:00
|
|
|
print("Got expected index out of bounds error on client side")
|