@@ -6,46 +6,61 @@ module Web
66 # Custom exceptions for clean error handling
77 # These map to HTTP status codes and are handled by Roda's error_handler
88
9- # HTTP 401 - Authentication required
10- class UnauthorizedError < StandardError
11- def initialize ( message = 'Authentication required' )
9+ class HttpError < StandardError
10+ DEFAULT_MESSAGE = 'Internal Server Error'
11+ STATUS = 500
12+ CODE = 'INTERNAL_SERVER_ERROR'
13+
14+ def initialize ( message = self . class ::DEFAULT_MESSAGE )
1215 super
1316 end
17+
18+ def status
19+ self . class ::STATUS
20+ end
21+
22+ def code
23+ self . class ::CODE
24+ end
25+ end
26+
27+ # HTTP 401 - Authentication required
28+ class UnauthorizedError < HttpError
29+ DEFAULT_MESSAGE = 'Authentication required'
30+ STATUS = 401
31+ CODE = 'UNAUTHORIZED'
1432 end
1533
1634 # HTTP 400 - Invalid request
17- class BadRequestError < StandardError
18- def initialize ( message = 'Bad Request' )
19- super
20- end
35+ class BadRequestError < HttpError
36+ DEFAULT_MESSAGE = 'Bad Request'
37+ STATUS = 400
38+ CODE = 'BAD_REQUEST'
2139 end
2240
2341 # HTTP 403 - Access denied
24- class ForbiddenError < StandardError
25- def initialize ( message = 'Forbidden' )
26- super
27- end
42+ class ForbiddenError < HttpError
43+ DEFAULT_MESSAGE = 'Forbidden'
44+ STATUS = 403
45+ CODE = 'FORBIDDEN'
2846 end
2947
3048 # HTTP 404 - Resource not found
31- class NotFoundError < StandardError
32- def initialize ( message = 'Not Found' )
33- super
34- end
49+ class NotFoundError < HttpError
50+ DEFAULT_MESSAGE = 'Not Found'
51+ STATUS = 404
52+ CODE = 'NOT_FOUND'
3553 end
3654
3755 # HTTP 405 - Method not allowed
38- class MethodNotAllowedError < StandardError
39- def initialize ( message = 'Method Not Allowed' )
40- super
41- end
56+ class MethodNotAllowedError < HttpError
57+ DEFAULT_MESSAGE = 'Method Not Allowed'
58+ STATUS = 405
59+ CODE = 'METHOD_NOT_ALLOWED'
4260 end
4361
4462 # HTTP 500 - Server error
45- class InternalServerError < StandardError
46- def initialize ( message = 'Internal Server Error' )
47- super
48- end
63+ class InternalServerError < HttpError
4964 end
5065 end
5166end
0 commit comments