Skip to content

Commit dfcf13d

Browse files
committed
always log internal server error
1 parent 353ce4d commit dfcf13d

File tree

3 files changed

+19
-19
lines changed

3 files changed

+19
-19
lines changed

README.md

+12-15
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,22 @@ Once you have a Mojo project set up locally,
9797
For example, to make a `Printer` service that prints some details about the request to console:
9898

9999
```mojo
100-
from lightbug_http import *
100+
from lightbug_http.http import HTTPRequest, HTTPResponse, OK
101+
from lightbug_http.strings import to_string
102+
from lightbug_http.header import HeaderKey
101103
102104
@value
103105
struct Printer(HTTPService):
104106
fn func(mut self, req: HTTPRequest) raises -> HTTPResponse:
105-
var uri = req.uri
106-
print("Request URI: ", to_string(uri.request_uri))
107-
108-
var header = req.headers
109-
print("Request protocol: ", req.protocol)
110-
print("Request method: ", req.method)
111-
print(
112-
"Request Content-Type: ", to_string(header[HeaderKey.CONTENT_TYPE])
113-
)
114-
115-
var body = req.body_raw
116-
print("Request Body: ", to_string(body))
117-
118-
return OK(body)
107+
print("Request URI:", req.uri.request_uri)
108+
print("Request protocol:", req.protocol)
109+
print("Request method:", req.method)
110+
if HeaderKey.CONTENT_TYPE in req.headers:
111+
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
112+
if req.body_raw:
113+
print("Request Body:", to_string(req.body_raw))
114+
115+
return OK(req.body_raw)
119116
```
120117

121118
6. Start a server listening on a port with your service like so.

lightbug_http/server.mojo

+3-2
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,15 @@ struct Server(Movable):
161161
var response: HTTPResponse
162162
try:
163163
response = handler.func(request)
164-
except:
164+
except e:
165+
logger.error("Unexpected error in the handler:", e)
166+
165167
if not conn.is_closed():
166168
# Try to send back an internal server error, but always attempt to teardown the connection.
167169
try:
168170
# TODO: Move InternalError response to an alias when Mojo can support Dict operations at compile time. (@thatstoasty)
169171
_ = conn.write(encode(InternalError()))
170172
except e:
171-
logger.error(e)
172173
raise Error("Failed to send InternalError response")
173174
finally:
174175
conn.teardown()

lightbug_http/service.mojo

+4-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ struct Printer(HTTPService):
1515
print("Request URI:", req.uri.request_uri)
1616
print("Request protocol:", req.protocol)
1717
print("Request method:", req.method)
18-
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
19-
print("Request Body:", to_string(req.body_raw))
18+
if HeaderKey.CONTENT_TYPE in req.headers:
19+
print("Request Content-Type:", req.headers[HeaderKey.CONTENT_TYPE])
20+
if req.body_raw:
21+
print("Request Body:", to_string(req.body_raw))
2022

2123
return OK(req.body_raw)
2224

0 commit comments

Comments
 (0)