Skip to content

Commit 2a4a658

Browse files
committed
Entity store HTTP API error responses as JSON
1 parent 59c0052 commit 2a4a658

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

crates/core/tedge_agent/src/http_server/entity_store.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ impl IntoResponse for Error {
126126
};
127127
let error_message = self.to_string();
128128

129-
(status_code, error_message).into_response()
129+
(status_code, Json(json!({ "error": error_message }))).into_response()
130130
}
131131
}
132132

@@ -319,6 +319,13 @@ mod tests {
319319

320320
let response = app.call(req).await.unwrap();
321321
assert_eq!(response.status(), StatusCode::NOT_FOUND);
322+
323+
let body = response.into_body().collect().await.unwrap().to_bytes();
324+
let entity: Value = serde_json::from_slice(&body).unwrap();
325+
assert_json_eq!(
326+
entity,
327+
json!( {"error":"Entity not found with topic id: device/test-child//"})
328+
);
322329
}
323330

324331
#[tokio::test]
@@ -410,6 +417,13 @@ mod tests {
410417

411418
let response = app.call(req).await.unwrap();
412419
assert_eq!(response.status(), StatusCode::CONFLICT);
420+
421+
let body = response.into_body().collect().await.unwrap().to_bytes();
422+
let entity: Value = serde_json::from_slice(&body).unwrap();
423+
assert_json_eq!(
424+
entity,
425+
json!( {"error":"An entity with topic id: device/test-child// is already registered"})
426+
);
413427
}
414428

415429
#[tokio::test]
@@ -456,6 +470,13 @@ mod tests {
456470

457471
let response = app.call(req).await.unwrap();
458472
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
473+
474+
let body = response.into_body().collect().await.unwrap().to_bytes();
475+
let entity: Value = serde_json::from_slice(&body).unwrap();
476+
assert_json_eq!(
477+
entity,
478+
json!( {"error":"Specified parent \"test-child\" does not exist in the store"})
479+
);
459480
}
460481

461482
#[tokio::test]
@@ -705,6 +726,13 @@ mod tests {
705726

706727
let response = app.call(req).await.unwrap();
707728
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
729+
730+
let body = response.into_body().collect().await.unwrap().to_bytes();
731+
let entity: Value = serde_json::from_slice(&body).unwrap();
732+
assert_json_eq!(
733+
entity,
734+
json!( {"error":"An entity topic identifier has at most 4 segments"})
735+
);
708736
}
709737

710738
#[tokio::test]
@@ -722,6 +750,13 @@ mod tests {
722750

723751
let response = app.call(req).await.unwrap();
724752
assert_eq!(response.status(), StatusCode::BAD_REQUEST);
753+
754+
let body = response.into_body().collect().await.unwrap().to_bytes();
755+
let entity: Value = serde_json::from_slice(&body).unwrap();
756+
assert_json_eq!(
757+
entity,
758+
json!( {"error":"The provided parameters: root and parent are mutually exclusive. Use either one."})
759+
);
725760
}
726761

727762
struct TestHandle {

tests/RobotFramework/tests/tedge/tedge_http.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Displaying server errors
4848
... tedge http post /tedge/entity-store/v1/entities '{"@topic-id": "device/a//", "@type": "child-device", "@parent": "device/unknown//"}' 2>&1
4949
... exp_exit_code=1
5050
Should Contain ${error_msg} 400 Bad Request
51-
Should Contain ${error_msg} Specified parent "device/unknown//" does not exist in the store
51+
Should Contain ${error_msg} Specified parent \\"device/unknown//\\" does not exist in the store
5252

5353

5454
*** Keywords ***

0 commit comments

Comments
 (0)