graylog stream handle error
Taylor committed
Sep 14, 2023 at 17:52 UTC
f5dfe024f0e2ebcacc82d22ffb2b23ed513ffab7
2 files changed
+35
-11
backend/app/routes/graylog.py
+26
-11
@@ -140,15 +140,21 @@ def get_inputs_configured() -> dict:
140
logger.info("Received request to get configured graylog inputs")
141
service = InputsService()
142
configured_inputs = service.collect_configured_inputs()
143
- for input in configured_inputs["configured_inputs"]:
144
- # Get the ID and invoke the get_inputstate function
145
- input_id = input["id"]
146
- inputstate = get_inputstate(input_id)
147
- # Add the inputstate to the configured_inputs
148
- input["inputstate"] = inputstate["inputstate"]["state"]
149
- return jsonify(
150
- {"configured_inputs": configured_inputs},
151
- )
143
+ try:
144
+ for input in configured_inputs["configured_inputs"]:
145
+ # Get the ID and invoke the get_inputstate function
146
+ input_id = input["id"]
147
+ inputstate = get_inputstate(input_id)
148
+ # Add the inputstate to the configured_inputs
149
+ input["inputstate"] = inputstate["inputstate"]["state"]
150
+ return jsonify(
151
+ {"configured_inputs": configured_inputs},
152
+ )
153
+ except Exception as e:
154
+ logger.error(e)
155
+ return jsonify(
156
+ {"message": "Error getting inputstate. Make sure Graylog is running and has valid credentials.", "success": False},
157
+ )
158
159
160
@bp.route("/graylog/inputs/<input_id>/stop", methods=["DELETE"])
@@ -251,8 +257,17 @@ def get_streams() -> dict:
257
"""
258
logger.info("Received request to get graylog streams")
259
service = StreamsService()
254
- streams = service.collect_streams()
255
- return streams
260
+ try:
261
+ streams = service.collect_streams()
262
+ return streams
263
+ except Exception as e:
264
+ logger.error(e)
265
+ return jsonify(
266
+ {
267
+ "message": "Error getting streams",
268
+ "success": False,
269
+ },
270
+ )
271
272
273
@bp.route("/graylog/streams/<stream_id>/pause", methods=["POST"])
backend/app/services/Graylog/streams.py
+9
@@ -44,6 +44,8 @@ class StreamsService:
44
45
if streams["success"]:
46
return streams
47
+ else:
48
+ return {"message": "Failed to collect streams", "success": False}
49
50
def _collect_streams(
51
self,
@@ -61,6 +63,13 @@ class StreamsService:
63
auth=(self.connector_username, self.connector_password),
64
verify=False,
65
)
66
+ # If streams is None, return an empty list
67
+ if streams is None:
68
+ return {
69
+ "message": "Collected empty list of streams",
70
+ "success": True,
71
+ "streams": [],
72
+ }
73
return {
74
"message": "Successfully collected streams",
75
"success": True,