Skip to content

Commit b4f78c3

Browse files
committed
Return nicer message based on return code
1 parent e576a1f commit b4f78c3

1 file changed

Lines changed: 48 additions & 3 deletions

File tree

src/onepassword/desktop_core.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import platform
55
import base64
66
from pathlib import Path
7-
from ctypes import c_uint8, c_size_t, c_int32, POINTER, byref, c_void_p
7+
import sys
8+
from ctypes import c_uint8, c_size_t, c_int32, POINTER, byref
89
from onepassword.errors import raise_typed_exception
910

1011

@@ -85,8 +86,9 @@ def call_shared_library(self, payload: str, operation_kind: str) -> bytes:
8586
byref(out_cap),
8687
)
8788

88-
if ret != 0:
89-
raise RuntimeError(f"send_message failed with code {ret}. In the 1Password desktop app, make sure Settings > Developer > Integrate with other apps is enabled, or contact 1Password support.")
89+
err = error_from_return_code(ret)
90+
if err is not None:
91+
raise err
9092

9193
# Copy bytes into Python
9294
data = ctypes.string_at(out_buf, out_len.value)
@@ -120,3 +122,46 @@ def release_client(self, client_id: int):
120122
self.call_shared_library(payload, "release_client")
121123
except Exception as e:
122124
print(f"failed to release client: {e}")
125+
126+
def error_from_return_code(ret_code: int) -> Exception | None:
127+
if ret_code == 0:
128+
return None
129+
130+
is_darwin = sys.platform == "darwin"
131+
132+
if is_darwin:
133+
if ret_code == -3:
134+
return RuntimeError(
135+
"desktop app connection channel is closed. "
136+
"Make sure Settings > Developer > Integrate with other apps is enabled, "
137+
"or contact 1Password support"
138+
)
139+
elif ret_code == -7:
140+
return RuntimeError(
141+
"connection was unexpectedly dropped by the desktop app. "
142+
"Make sure the desktop app is running and Settings > Developer > "
143+
"Integrate with other apps is enabled, or contact 1Password support"
144+
)
145+
else:
146+
return RuntimeError(
147+
f"an internal error occurred. Please contact 1Password support "
148+
f"and mention the return code: {ret_code}"
149+
)
150+
else:
151+
if ret_code == -2:
152+
return RuntimeError(
153+
"desktop app connection channel is closed. "
154+
"Make sure Settings > Developer > Integrate with other apps is enabled, "
155+
"or contact 1Password support"
156+
)
157+
elif ret_code == -5:
158+
return RuntimeError(
159+
"connection was unexpectedly dropped by the desktop app. "
160+
"Make sure the desktop app is running and Settings > Developer > "
161+
"Integrate with other apps is enabled, or contact 1Password support"
162+
)
163+
else:
164+
return RuntimeError(
165+
f"an internal error occurred. Please contact 1Password support "
166+
f"and mention the return code: {ret_code}"
167+
)

0 commit comments

Comments
 (0)