Skip to content

Commit

Permalink
refactor: error messages inprovement, closes #299
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrdjan committed Feb 10, 2023
1 parent 0c31aae commit e5a346f
Show file tree
Hide file tree
Showing 5 changed files with 4,497 additions and 4,358 deletions.
94 changes: 94 additions & 0 deletions examples/server/servebgrfc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
from pyrfc import Server, set_ini_file_directory
from threading import Thread

# server functions


def my_stfc_connection(request_context=None, REQUTEXT=""):
print("stfc connection invoked")
print("request_context", request_context)
print(f"REQUTEXT: {REQUTEXT}")

return {"ECHOTEXT": REQUTEXT, "RESPTEXT": "Python server here"}


def my_stfc_structure(request_context=None, IMPORTSTRUCT={}, RFCTABLE=[]):
print("stfc structure invoked")
print("request_context", request_context)
ECHOSTRUCT = IMPORTSTRUCT
if len(RFCTABLE) == 0:
RFCTABLE = [ECHOSTRUCT]
RESPTEXT = f"Python server response: {ECHOSTRUCT['RFCINT1']}, table rows: {len(RFCTABLE)}"
print(f"ECHOSTRUCT: {ECHOSTRUCT}")
print(f"RFCTABLE: {RFCTABLE}")
print(f"RESPTEXT: {RESPTEXT}")

return {"ECHOSTRUCT": ECHOSTRUCT, "RFCTABLE": RFCTABLE, "RESPTEXT": RESPTEXT}


# server authorisation check


def my_auth_check(func_name=False, request_context={}):
print(f"authorization check for '{func_name}'")
print("request_context", request_context)
return 0


dir_path = os.path.dirname(os.path.realpath(__file__))
set_ini_file_directory(dir_path)


def myCheckFunction(rfcHandle, unit_identifier):
print("myCheckFunction", rfcHandle, unit_identifier)
return 3


def myCommitFunction(rfcHandle, unit_identifier):
print("myCommitFunction", rfcHandle, unit_identifier)
return 2


def server1_serve():
# create server for ABAP system ABC
server = Server({"dest": "gateway"}, {"dest": "MME"}, {"port": 8081, "server_log": False})
print(server.get_server_attributes())

# expose python function my_stfc_connection as ABAP function STFC_CONNECTION, to be called by ABAP system
server.add_function("STFC_CONNECTION", my_stfc_connection)

server.bgrfc_init("NODEJS", {"check": myCheckFunction, "commit": myCommitFunction})

# start server
server.serve()


def server2_serve():
# create server for ABAP system XYZ
server = Server({"dest": "gatewayqm7"}, {"dest": "QM7"}, {"port": 8081, "server_log": False})
print(server.get_server_attributes())

# expose python function my_stfc_structure as ABAP function STFC_STRUCTURE, to be called by ABAP system
server.add_function("STFC_STRUCTURE", my_stfc_structure)

# start server
server.serve()


# start servers

server1_thread = Thread(target=server1_serve)
server1_thread.start()
print("Server1 started")

# server2_thread = Thread(target=server2_serve)
# server2_thread.start()
# print("Server2 started")

input("Press Enter to stop server1...")

server1_thread.join()

# input("Press Enter to stop server2...")
# server2_thread.join()
79 changes: 36 additions & 43 deletions src/pyrfc/_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,36 @@ class RFCLibError(RFCError):
# Results as of: RfcGetRcAsString(RFC_RC rc)
# cf. material/kjona_getRcAsString
code2txt = {
0: u"RFC_OK",
1: u"RFC_COMMUNICATION_FAILURE",
2: u"RFC_LOGON_FAILURE",
3: u"RFC_ABAP_RUNTIME_FAILURE",
4: u"RFC_ABAP_MESSAGE",
5: u"RFC_ABAP_EXCEPTION",
6: u"RFC_CLOSED",
7: u"RFC_CANCELED",
8: u"RFC_TIMEOUT",
9: u"RFC_MEMORY_INSUFFICIENT",
10: u"RFC_VERSION_MISMATCH",
11: u"RFC_INVALID_PROTOCOL",
12: u"RFC_SERIALIZATION_FAILURE",
13: u"RFC_INVALID_HANDLE",
14: u"RFC_RETRY",
15: u"RFC_EXTERNAL_FAILURE",
16: u"RFC_EXECUTED",
17: u"RFC_NOT_FOUND",
18: u"RFC_NOT_SUPPORTED",
19: u"RFC_ILLEGAL_STATE",
20: u"RFC_INVALID_PARAMETER",
21: u"RFC_CODEPAGE_CONVERSION_FAILURE",
22: u"RFC_CONVERSION_FAILURE",
23: u"RFC_BUFFER_TOO_SMALL",
24: u"RFC_TABLE_MOVE_BOF",
25: u"RFC_TABLE_MOVE_EOF",
26: u"RFC_START_SAPGUI_FAILURE",
27: u"RFC_ABAP_CLASS_EXCEPTION",
28: u"RFC_UNKNOWN_ERROR",
29: u"RFC_AUTHORIZATION_FAILURE",
0: "RFC_OK",
1: "RFC_COMMUNICATION_FAILURE",
2: "RFC_LOGON_FAILURE",
3: "RFC_ABAP_RUNTIME_FAILURE",
4: "RFC_ABAP_MESSAGE",
5: "RFC_ABAP_EXCEPTION",
6: "RFC_CLOSED",
7: "RFC_CANCELED",
8: "RFC_TIMEOUT",
9: "RFC_MEMORY_INSUFFICIENT",
10: "RFC_VERSION_MISMATCH",
11: "RFC_INVALID_PROTOCOL",
12: "RFC_SERIALIZATION_FAILURE",
13: "RFC_INVALID_HANDLE",
14: "RFC_RETRY",
15: "RFC_EXTERNAL_FAILURE",
16: "RFC_EXECUTED",
17: "RFC_NOT_FOUND",
18: "RFC_NOT_SUPPORTED",
19: "RFC_ILLEGAL_STATE",
20: "RFC_INVALID_PARAMETER",
21: "RFC_CODEPAGE_CONVERSION_FAILURE",
22: "RFC_CONVERSION_FAILURE",
23: "RFC_BUFFER_TOO_SMALL",
24: "RFC_TABLE_MOVE_BOF",
25: "RFC_TABLE_MOVE_EOF",
26: "RFC_START_SAPGUI_FAILURE",
27: "RFC_ABAP_CLASS_EXCEPTION",
28: "RFC_UNKNOWN_ERROR",
29: "RFC_AUTHORIZATION_FAILURE",
}

def __init__(
Expand Down Expand Up @@ -82,18 +82,11 @@ def __init__(
self.msg_v4 = msg_v4

def __str__(self):
return "{} (rc={}): key={}, message={} [MSG: class={}, type={}, number={}, v1-4:={};{};{};{}]".format(
self.code2txt.get(self.code, u"???"),
self.code,
self.key,
self.message,
self.msg_class,
self.msg_type,
self.msg_number,
self.msg_v1,
self.msg_v2,
self.msg_v3,
self.msg_v4,
rctext = self.code2txt.get(28 if self.code is None else self.code, "???")
return (
f"{rctext} (rc={self.code}): key={self.key}, message={self.message}"
f" [MSG: class={self.msg_class}, type={self.msg_type}, number={self.msg_number},"
f" v1-4:={self.msg_v1};{self.msg_v2};{self.msg_v3};{self.msg_v4}]"
)


Expand Down Expand Up @@ -131,7 +124,7 @@ def __init__(
self,
message=None,
code=2,
key=u"RFC_LOGON_FAILURE",
key="RFC_LOGON_FAILURE",
msg_class=None,
msg_type=None,
msg_number=None,
Expand Down
Loading

0 comments on commit e5a346f

Please sign in to comment.