Skip to content

Commit 0cc331d

Browse files
committed
refac
1 parent 41f2563 commit 0cc331d

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

backend/open_webui/constants.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,29 @@
11
from __future__ import annotations
22

3+
import errno
34
from enum import Enum
45

56

7+
_ERRNO_MESSAGES = {
8+
errno.ENAMETOOLONG: 'File name is too long.',
9+
errno.ENOSPC: 'The server is out of storage space.',
10+
errno.EDQUOT: 'Server storage quota exceeded.',
11+
errno.EACCES: 'Server storage is not writable.',
12+
errno.EPERM: 'Server storage is not writable.',
13+
errno.EROFS: 'Server storage is not writable.',
14+
}
15+
16+
17+
def _error_message(err='', fallback='') -> str:
18+
if not err:
19+
return 'Something went wrong :/'
20+
if isinstance(err, OSError) and err.errno in _ERRNO_MESSAGES:
21+
return f'[ERROR: {_ERRNO_MESSAGES[err.errno]}]'
22+
if isinstance(err, Exception):
23+
return f'[ERROR: {fallback}]' if fallback else 'Something went wrong :/'
24+
return f'[ERROR: {err}]'
25+
26+
627
class MESSAGES(str, Enum):
728
DEFAULT = lambda msg='': f'{msg if msg else ""}'
829
MODEL_ADDED = lambda model='': f"The model '{model}' has been added successfully."
@@ -18,7 +39,7 @@ class ERROR_MESSAGES(str, Enum):
1839
def __str__(self) -> str:
1940
return super().__str__()
2041

21-
DEFAULT = lambda err='': f'{"Something went wrong :/" if err == "" else "[ERROR: " + str(err) + "]"}'
42+
DEFAULT = _error_message
2243
ENV_VAR_NOT_FOUND = 'Required environment variable not found. Terminating now.'
2344
CREATE_USER_ERROR = 'Oops! Something went wrong while creating your account. Please try again later. If the issue persists, contact support for assistance.'
2445
DELETE_USER_ERROR = 'Oops! Something went wrong. We encountered an issue while trying to delete the user. Please give it another shot.'

0 commit comments

Comments
 (0)