Skip to content

Commit

Permalink
Entity store HTTP API error responses as JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
albinsuresh committed Feb 10, 2025
1 parent 59c0052 commit 2a4a658
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
37 changes: 36 additions & 1 deletion crates/core/tedge_agent/src/http_server/entity_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl IntoResponse for Error {
};
let error_message = self.to_string();

(status_code, error_message).into_response()
(status_code, Json(json!({ "error": error_message }))).into_response()
}
}

Expand Down Expand Up @@ -319,6 +319,13 @@ mod tests {

let response = app.call(req).await.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);

let body = response.into_body().collect().await.unwrap().to_bytes();
let entity: Value = serde_json::from_slice(&body).unwrap();
assert_json_eq!(
entity,
json!( {"error":"Entity not found with topic id: device/test-child//"})
);
}

#[tokio::test]
Expand Down Expand Up @@ -410,6 +417,13 @@ mod tests {

let response = app.call(req).await.unwrap();
assert_eq!(response.status(), StatusCode::CONFLICT);

let body = response.into_body().collect().await.unwrap().to_bytes();
let entity: Value = serde_json::from_slice(&body).unwrap();
assert_json_eq!(
entity,
json!( {"error":"An entity with topic id: device/test-child// is already registered"})
);
}

#[tokio::test]
Expand Down Expand Up @@ -456,6 +470,13 @@ mod tests {

let response = app.call(req).await.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);

let body = response.into_body().collect().await.unwrap().to_bytes();
let entity: Value = serde_json::from_slice(&body).unwrap();
assert_json_eq!(
entity,
json!( {"error":"Specified parent \"test-child\" does not exist in the store"})
);
}

#[tokio::test]
Expand Down Expand Up @@ -705,6 +726,13 @@ mod tests {

let response = app.call(req).await.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);

let body = response.into_body().collect().await.unwrap().to_bytes();
let entity: Value = serde_json::from_slice(&body).unwrap();
assert_json_eq!(
entity,
json!( {"error":"An entity topic identifier has at most 4 segments"})
);
}

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

let response = app.call(req).await.unwrap();
assert_eq!(response.status(), StatusCode::BAD_REQUEST);

let body = response.into_body().collect().await.unwrap().to_bytes();
let entity: Value = serde_json::from_slice(&body).unwrap();
assert_json_eq!(
entity,
json!( {"error":"The provided parameters: root and parent are mutually exclusive. Use either one."})
);
}

struct TestHandle {
Expand Down
2 changes: 1 addition & 1 deletion tests/RobotFramework/tests/tedge/tedge_http.robot
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Displaying server errors
... tedge http post /tedge/entity-store/v1/entities '{"@topic-id": "device/a//", "@type": "child-device", "@parent": "device/unknown//"}' 2>&1
... exp_exit_code=1
Should Contain ${error_msg} 400 Bad Request
Should Contain ${error_msg} Specified parent "device/unknown//" does not exist in the store
Should Contain ${error_msg} Specified parent \\"device/unknown//\\" does not exist in the store


*** Keywords ***
Expand Down

0 comments on commit 2a4a658

Please sign in to comment.