-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon_errors.h
More file actions
121 lines (102 loc) · 4.56 KB
/
common_errors.h
File metadata and controls
121 lines (102 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#ifndef COMMON_ERRORS_H
#define COMMON_ERRORS_H
/**
* Universal Error Codes for Distributed File System
* These codes are used across all components: Client, Name Server, and Storage Server
*/
// Success codes (200-299)
#define ERR_SUCCESS 200
#define ERR_CREATED 201
#define ERR_ACCEPTED 202
// Client errors (400-499)
#define ERR_BAD_REQUEST 400
#define ERR_UNAUTHORIZED 401
#define ERR_FORBIDDEN 403
#define ERR_NOT_FOUND 404
#define ERR_CONFLICT 409
#define ERR_LOCKED 423 // File locked for writing
#define ERR_INVALID_SENTENCE 424 // Invalid sentence index
#define ERR_INVALID_WORD 425 // Invalid word index
// Server errors (500-599)
#define ERR_INTERNAL_ERROR 500
#define ERR_NOT_IMPLEMENTED 501
#define ERR_SERVICE_UNAVAILABLE 503
#define ERR_TIMEOUT 504
// Storage Server specific errors (600-699)
#define ERR_SS_NOT_AVAILABLE 600
#define ERR_SS_COMMUNICATION_FAILED 601
#define ERR_SS_FILE_IO_ERROR 602
#define ERR_SS_MEMORY_ERROR 603
#define ERR_SS_LOCK_FAILED 604
// Name Server specific errors (700-799)
#define ERR_NM_FILE_EXISTS 700
#define ERR_NM_SS_NOT_REGISTERED 701
#define ERR_NM_DUPLICATE_USERNAME 702
#define ERR_NM_INVALID_PERMISSION 703
// Network/Communication errors (800-899)
#define ERR_NETWORK_SEND_FAILED 800
#define ERR_NETWORK_RECV_FAILED 801
#define ERR_CONNECTION_LOST 802
#define ERR_INVALID_RESPONSE 803
/**
* Error message lookup
* Returns human-readable error message for given error code
*/
static inline const char* get_error_message(int error_code) {
switch (error_code) {
// Success codes
case ERR_SUCCESS: return "Operation successful";
case ERR_CREATED: return "Resource created successfully";
case ERR_ACCEPTED: return "Request accepted";
// Client errors
case ERR_BAD_REQUEST: return "Invalid request format";
case ERR_UNAUTHORIZED: return "Authentication required";
case ERR_FORBIDDEN: return "Permission denied";
case ERR_NOT_FOUND: return "Resource not found";
case ERR_CONFLICT: return "Resource conflict";
case ERR_LOCKED: return "Resource is locked by another operation";
case ERR_INVALID_SENTENCE: return "Invalid sentence index";
case ERR_INVALID_WORD: return "Invalid word index";
// Server errors
case ERR_INTERNAL_ERROR: return "Internal server error";
case ERR_NOT_IMPLEMENTED: return "Feature not implemented";
case ERR_SERVICE_UNAVAILABLE: return "Service temporarily unavailable";
case ERR_TIMEOUT: return "Request timeout";
// Storage Server errors
case ERR_SS_NOT_AVAILABLE: return "Storage server not available";
case ERR_SS_COMMUNICATION_FAILED: return "Storage server communication failed";
case ERR_SS_FILE_IO_ERROR: return "Storage server file I/O error";
case ERR_SS_MEMORY_ERROR: return "Storage server memory allocation failed";
case ERR_SS_LOCK_FAILED: return "Failed to acquire file lock";
// Name Server errors
case ERR_NM_FILE_EXISTS: return "File already exists";
case ERR_NM_SS_NOT_REGISTERED: return "No storage server registered";
case ERR_NM_DUPLICATE_USERNAME: return "Username already exists";
case ERR_NM_INVALID_PERMISSION: return "Invalid permission level";
// Network errors
case ERR_NETWORK_SEND_FAILED: return "Failed to send data";
case ERR_NETWORK_RECV_FAILED: return "Failed to receive data";
case ERR_CONNECTION_LOST: return "Connection lost";
case ERR_INVALID_RESPONSE: return "Invalid server response";
default: return "Unknown error";
}
}
/**
* Check if error code indicates success
*/
static inline int is_success(int error_code) {
return error_code >= 200 && error_code < 300;
}
/**
* Check if error code indicates client error
*/
static inline int is_client_error(int error_code) {
return error_code >= 400 && error_code < 500;
}
/**
* Check if error code indicates server error
*/
static inline int is_server_error(int error_code) {
return error_code >= 500 && error_code < 600;
}
#endif // COMMON_ERRORS_H