Skip to content

Commit

Permalink
fix: Make the body of the error response into json (#402)
Browse files Browse the repository at this point in the history
* define AgentError and use it for network

* rename enum

* fix versions

* change error structure

* apply errors to create didcomm message

* apply errors to create identifier

* apply errors to create verifiable message

* apply errors to find identifier

* apply errors to verify didcomm message

* apply errors to verify verifiable message

* rename file

* apply errors to send attribute

* lint fix

* apply errors to send custom metric

* apply errors to send event

* apply errors to utils

* Add 2 errors

* destination -> target
  • Loading branch information
ibuibu authored Oct 10, 2024
1 parent ca115ed commit 1f8bcbd
Show file tree
Hide file tree
Showing 16 changed files with 295 additions and 69 deletions.
7 changes: 5 additions & 2 deletions agent/src/controllers/internal/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::services::studio::Studio;
use crate::{
errors::{create_agent_error, AgentErrorCode},
services::studio::Studio,
};
use actix_web::{web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};
use serde_json::Value;
Expand All @@ -18,7 +21,7 @@ pub async fn handler(
Ok(_) => Ok(HttpResponse::Ok().json("ok")),
Err(e) => {
log::error!("{:?}", e);
Ok(HttpResponse::InternalServerError().json("Internal Server Error"))
Ok(create_agent_error(AgentErrorCode::NetworkInternal))
}
}
}
11 changes: 7 additions & 4 deletions agent/src/controllers/internal/version.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::services::nodex::NodeX;
use crate::{
errors::{create_agent_error, AgentErrorCode},
services::nodex::NodeX,
};
use actix_web::{web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
Expand All @@ -21,15 +24,15 @@ pub async fn handler_update(
) -> actix_web::Result<HttpResponse> {
let binary_url = match json.message["binary_url"].as_str() {
Some(url) => url,
None => return Ok(HttpResponse::BadRequest().json("binary_url is required")),
None => return Ok(create_agent_error(AgentErrorCode::VersionNoBinaryUrl)),
};
let path = match json.message["path"].as_str() {
Some(p) => p,
None => return Ok(HttpResponse::BadRequest().json("path is required")),
None => return Ok(create_agent_error(AgentErrorCode::VersionNoPath)),
};
let nodex = NodeX::new();
match nodex.update_version(binary_url, PathBuf::from(path)).await {
Ok(_) => Ok(HttpResponse::Ok().json("ok")),
Err(_) => Ok(HttpResponse::InternalServerError().finish()),
Err(_) => Ok(create_agent_error(AgentErrorCode::VersionInternal)),
}
}
2 changes: 1 addition & 1 deletion agent/src/controllers/public/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub mod nodex_find_identifier;
pub mod nodex_receive;
pub mod nodex_verify_didcomm_message;
pub mod nodex_verify_verifiable_message;
pub mod send_attributes;
pub mod send_attribute;
pub mod send_custom_metric;
pub mod send_event;
mod utils;
31 changes: 22 additions & 9 deletions agent/src/controllers/public/nodex_create_didcomm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use serde::{Deserialize, Serialize};

use protocol::didcomm::encrypted::DidCommEncryptedServiceGenerateError as S;

use crate::errors::{create_agent_error, AgentErrorCode};
use crate::nodex::utils::did_accessor::DidAccessorImpl;
use crate::usecase::didcomm_message_usecase::GenerateDidcommMessageUseCaseError as U;
use crate::{services::studio::Studio, usecase::didcomm_message_usecase::DidcommMessageUseCase};
Expand Down Expand Up @@ -35,28 +36,40 @@ pub async fn handler(
Err(e) => match e {
U::MessageActivity(e) => Ok(utils::handle_status(e)),
U::ServiceGenerate(S::DidDocNotFound(target)) => {
log::warn!("Target DID not found. did = {}", target);
Ok(HttpResponse::NotFound().finish())
log::warn!("target DID not found. did = {}", target);
Ok(create_agent_error(
AgentErrorCode::CreateDidCommMessageNoDid,
))
}
U::ServiceGenerate(S::DidPublicKeyNotFound(e)) => {
log::warn!("cannot public key: {}", e);
Ok(HttpResponse::BadRequest().body(e.to_string()))
log::warn!("cannot find public key: {}", e);
Ok(create_agent_error(
AgentErrorCode::CreateDidCommMessageNoPubKey,
))
}
U::Json(e) | U::ServiceGenerate(S::Json(e)) => {
log::warn!("json error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::CreateDidcommMessageInternal,
))
}
U::ServiceGenerate(S::VcService(e)) => {
log::warn!("verify error: {}", e);
Ok(HttpResponse::Unauthorized().finish())
log::warn!("verify failed: {}", e);
Ok(create_agent_error(
AgentErrorCode::CreateDidCommMessageVerifyFailed,
))
}
U::ServiceGenerate(S::SidetreeFindRequestFailed(e)) => {
log::warn!("sidetree error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::CreateDidcommMessageInternal,
))
}
U::ServiceGenerate(S::EncryptFailed(e)) => {
log::warn!("decrypt failed: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::CreateDidcommMessageInternal,
))
}
},
}
Expand Down
4 changes: 3 additions & 1 deletion agent/src/controllers/public/nodex_create_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use actix_web::{HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};

use crate::errors::{create_agent_error, AgentErrorCode};

// NOTE: POST /identifiers
#[derive(Deserialize, Serialize)]
struct MessageContainer {}
Expand All @@ -12,7 +14,7 @@ pub async fn handler(_req: HttpRequest) -> actix_web::Result<HttpResponse> {
Ok(v) => Ok(HttpResponse::Ok().json(&v)),
Err(e) => {
log::error!("{:?}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(AgentErrorCode::CreateIdentifierInternal))
}
}
}
13 changes: 10 additions & 3 deletions agent/src/controllers/public/nodex_create_verifiable_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use actix_web::{web, HttpRequest, HttpResponse};
use chrono::Utc;
use serde::{Deserialize, Serialize};

use crate::errors::{create_agent_error, AgentErrorCode};
use crate::nodex::utils::did_accessor::DidAccessorImpl;
use crate::usecase::verifiable_message_usecase::CreateVerifiableMessageUseCaseError as U;
use crate::{
Expand Down Expand Up @@ -39,15 +40,21 @@ pub async fn handler(
if let Some(e) = e {
log::error!("{:?}", e);
}
Ok(HttpResponse::NotFound().finish())
Ok(create_agent_error(
AgentErrorCode::CreateVerifiableMessageNoTargetDid,
))
}
U::DidVcServiceGenerate(e) => {
log::error!("{:?}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::CreateVerifiableMessageInternal,
))
}
U::Json(e) => {
log::warn!("json error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::CreateVerifiableMessageInternal,
))
}
},
}
Expand Down
4 changes: 3 additions & 1 deletion agent/src/controllers/public/nodex_find_identifier.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use actix_web::{web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};

use crate::errors::{create_agent_error, AgentErrorCode};

// NOTE: GET /identifiers/${ did }
#[derive(Deserialize, Serialize)]
struct MessageContainer {}
Expand All @@ -12,7 +14,7 @@ pub async fn handler(_req: HttpRequest, did: web::Path<String>) -> actix_web::Re
Ok(v) => Ok(HttpResponse::Ok().json(&v)),
Err(e) => {
log::error!("{:?}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(AgentErrorCode::FindIdentifierInternal))
}
}
}
54 changes: 39 additions & 15 deletions agent/src/controllers/public/nodex_verify_didcomm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use protocol::didcomm::encrypted::DidCommEncryptedServiceVerifyError as S;
use protocol::didcomm::types::DidCommMessage;

use crate::errors::{create_agent_error, AgentErrorCode};
use crate::nodex::utils::did_accessor::DidAccessorImpl;
use crate::{
services::studio::Studio,
Expand All @@ -31,47 +32,70 @@ pub async fn handler(
DidcommMessageUseCase::new(Studio::new(), utils::did_repository(), DidAccessorImpl {});

match serde_json::from_str::<DidCommMessage>(&json.message) {
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
Err(e) => {
log::warn!("json error: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageJsonError,
))
}
Ok(message) => match usecase.verify(message, now).await {
Ok(v) => Ok(HttpResponse::Ok().json(v)),
Err(e) => match e {
U::MessageActivity(e) => Ok(utils::handle_status(e)),
U::NotAddressedToMe => {
log::warn!("its not to me: {}", e);
Ok(HttpResponse::Forbidden().finish())
log::warn!("this message is not addressed to me: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageNotAddressedToMe,
))
}
U::ServiceVerify(S::FindSender(e)) => {
log::warn!("cannot find sender: {}", e);
Ok(HttpResponse::BadRequest().body(e.to_string()))
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageNoSender,
))
}
U::ServiceVerify(S::DidPublicKeyNotFound(e)) => {
log::warn!("cannot public key: {}", e);
Ok(HttpResponse::BadRequest().body(e.to_string()))
log::warn!("cannot find public key: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageNoPublicKey,
))
}
U::ServiceVerify(S::MetadataBodyNotFound(e)) => {
let e = e.map(|e| e.to_string()).unwrap_or("".to_string());
log::warn!("cannot find sender: {}", e);
Ok(HttpResponse::BadRequest().body(e))
log::warn!("cannot find metadata: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageNoMetadata,
))
}
U::ServiceVerify(S::VcService(e)) => {
log::warn!("verify error: {}", e);
Ok(HttpResponse::Unauthorized().finish())
log::warn!("verify failed: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageVerifyFailed,
))
}
U::ServiceVerify(S::DidDocNotFound(target)) => {
log::warn!("Target DID not found. did = {}", target);
Ok(HttpResponse::NotFound().finish())
log::warn!("target DID not found. DID = {}", target);
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageNoTargetDid,
))
}
U::Json(e) | U::ServiceVerify(S::Json(e)) => {
log::warn!("json error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageInternal,
))
}
U::ServiceVerify(S::DecryptFailed(e)) => {
log::warn!("decrypt failed: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageInternal,
))
}
U::ServiceVerify(S::SidetreeFindRequestFailed(e)) => {
log::warn!("sidetree error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::VerifyDidcommMessageInternal,
))
}
},
},
Expand Down
41 changes: 31 additions & 10 deletions agent/src/controllers/public/nodex_verify_verifiable_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use serde::{Deserialize, Serialize};
use protocol::verifiable_credentials::did_vc::DidVcServiceVerifyError as S;
use protocol::verifiable_credentials::types::VerifiableCredentials;

use crate::errors::{create_agent_error, AgentErrorCode};
use crate::nodex::utils::did_accessor::DidAccessorImpl;
use crate::usecase::verifiable_message_usecase::VerifyVerifiableMessageUseCaseError as U;
use crate::{
Expand All @@ -30,31 +31,51 @@ pub async fn handler(
VerifiableMessageUseCase::new(Studio::new(), repo.clone(), DidAccessorImpl {}, repo);

match serde_json::from_str::<VerifiableCredentials>(&json.message) {
Err(e) => Ok(HttpResponse::BadRequest().body(e.to_string())),
Err(e) => {
log::warn!("json error: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageJsonError,
))
}
Ok(vc) => match usecase.verify(vc, now).await {
Ok(v) => Ok(HttpResponse::Ok().json(v)),
Err(e) => match e {
U::MessageActivity(e) => Ok(utils::handle_status(e)),
U::DidVcServiceVerify(S::VerifyFailed(e)) => {
log::warn!("verify error: {}", e);
Ok(HttpResponse::Unauthorized().finish())
log::warn!("verify failed: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageVerifyFailed,
))
}
U::DidVcServiceVerify(S::FindIdentifier(e)) => {
log::warn!("find identifier error: {}", e);
Ok(HttpResponse::NotFound().finish())
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageNoIdentifier,
))
}
U::DidVcServiceVerify(S::DidDocNotFound(target)) => {
log::warn!("Target DID not found. did = {}", target);
Ok(HttpResponse::NotFound().finish())
log::warn!("target DID not found. DID = {}", target);
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageNoTargetDid,
))
}
U::NotAddressedToMe => {
log::warn!("this message is not addressed to me: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageNotAddressedToMe,
))
}
U::NotAddressedToMe => Ok(HttpResponse::Forbidden().finish()),
U::Json(e) => {
log::warn!("json error: {}", e);
Ok(HttpResponse::InternalServerError().finish())
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageInternal,
))
}
U::DidVcServiceVerify(S::PublicKeyNotFound(e)) => {
log::warn!("cannot public key: {}", e);
Ok(HttpResponse::BadRequest().body(e.to_string()))
log::warn!("cannot find public key: {}", e);
Ok(create_agent_error(
AgentErrorCode::VerifyVerifiableMessageNoPublicKey,
))
}
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use actix_web::{web, HttpRequest, HttpResponse};
use serde::{Deserialize, Serialize};

use crate::{
errors::{create_agent_error, AgentErrorCode},
repository::attribute_repository::AttributeStoreRequest,
usecase::attribute_usecase::AttributeUsecase,
};
Expand All @@ -17,10 +18,10 @@ pub async fn handler(
web::Json(json): web::Json<MessageContainer>,
) -> actix_web::Result<HttpResponse> {
if json.key_name.is_empty() {
return Ok(HttpResponse::BadRequest().json("key_name is required"));
return Ok(create_agent_error(AgentErrorCode::SendAttributeNoKeyName));
}
if json.value.is_empty() {
return Ok(HttpResponse::BadRequest().json("value is required"));
return Ok(create_agent_error(AgentErrorCode::SendAttributeNoValue));
}

let usecase = AttributeUsecase::new();
Expand All @@ -37,7 +38,7 @@ pub async fn handler(
}
Err(e) => {
log::error!("{:?}", e);
Ok(HttpResponse::InternalServerError().json("internal server error"))
Ok(create_agent_error(AgentErrorCode::SendAttributeInternal))
}
}
}
Loading

0 comments on commit 1f8bcbd

Please sign in to comment.