11use crate :: {
2- command:: server:: { self , ComponentConfigRegistryRO , SubmitError } ,
2+ command:: server:: { self , ComponentConfigRegistryRO , SubmitError , SubmitOutcome } ,
33 server:: web_api:: components:: { component_wit, components_list} ,
44} ;
55use axum:: {
@@ -15,9 +15,9 @@ use concepts::{
1515 component_id:: InputContentDigest ,
1616 prefixed_ulid:: { DelayId , ExecutionIdDerived } ,
1717 storage:: {
18- self , CancelOutcome , DbErrorGeneric , DbErrorRead , DbErrorWrite , DbErrorWriteNonRetriable ,
19- DbPool , ExecutionListPagination , ExecutionRequest , ExecutionWithState , Pagination ,
20- PendingState , Version , VersionType ,
18+ self , CancelOutcome , DbErrorGeneric , DbErrorRead , DbErrorWrite , DbPool ,
19+ ExecutionListPagination , ExecutionRequest , ExecutionWithState , Pagination , PendingState ,
20+ Version , VersionType ,
2121 } ,
2222 time:: { ClockFn as _, Now } ,
2323} ;
@@ -49,7 +49,7 @@ fn v1_router() -> Router<Arc<WebApiState>> {
4949 . route ( "/delays/{delay-id}/cancel" , routing:: put ( delay_cancel) )
5050 . route ( "/execution-id" , routing:: get ( execution_id_generate) )
5151 . route ( "/executions" , routing:: get ( executions_list) )
52- . route ( "/executions/submit " , routing:: post ( execution_submit_post) )
52+ . route ( "/executions" , routing:: post ( execution_submit_post) )
5353 . route (
5454 "/executions/{execution-id}/cancel" ,
5555 routing:: put ( execution_cancel) ,
@@ -513,13 +513,7 @@ async fn execution_submit_put(
513513 accept : AcceptHeader ,
514514 Json ( payload) : Json < ExecutionPutPayload > ,
515515) -> Result < Response , HttpResponse > {
516- execution_submit ( execution_id. clone ( ) , state, payload, accept) . await ?;
517- Ok ( HttpResponse {
518- status : StatusCode :: CREATED ,
519- message : execution_id. to_string ( ) ,
520- accept,
521- }
522- . into_response ( ) )
516+ execution_submit ( execution_id, state, payload, accept) . await
523517}
524518
525519async fn execution_submit_post (
@@ -528,43 +522,40 @@ async fn execution_submit_post(
528522 Json ( payload) : Json < ExecutionPutPayload > ,
529523) -> Result < Response , HttpResponse > {
530524 let execution_id = ExecutionId :: generate ( ) ;
531- execution_submit ( execution_id. clone ( ) , state, payload, accept) . await ?;
532- Ok ( HttpResponse {
533- status : StatusCode :: CREATED ,
534- message : execution_id. to_string ( ) ,
535- accept,
536- }
537- . into_response ( ) )
525+ execution_submit ( execution_id, state, payload, accept) . await
538526}
539527
540528async fn execution_submit (
541529 execution_id : ExecutionId ,
542530 state : State < Arc < WebApiState > > ,
543531 payload : ExecutionPutPayload ,
544532 accept : AcceptHeader ,
545- ) -> Result < ( ) , HttpResponse > {
546- server:: submit (
547- state. db_pool . connection ( ) . as_ref ( ) ,
548- execution_id,
533+ ) -> Result < Response , HttpResponse > {
534+ let conn = state. db_pool . connection ( ) ;
535+ let res = server:: submit (
536+ conn. as_ref ( ) ,
537+ execution_id. clone ( ) ,
549538 payload. ffqn ,
550539 payload. params ,
551540 & state. component_registry_ro ,
552541 )
553- . await
554- . map_err ( |err| match err {
555- SubmitError :: DbErrorWrite ( DbErrorWrite :: NonRetriable (
556- DbErrorWriteNonRetriable :: Conflict ,
557- ) ) => HttpResponse {
558- status : StatusCode :: CONFLICT ,
559- message : "already exists" . to_string ( ) ,
542+ . await ;
543+
544+ match res {
545+ Ok ( SubmitOutcome :: Created ) => Ok ( HttpResponse {
546+ status : StatusCode :: CREATED ,
547+ message : execution_id. to_string ( ) ,
560548 accept,
561- } ,
562- err => HttpResponse {
563- status : StatusCode :: INTERNAL_SERVER_ERROR ,
564- message : err. to_string ( ) ,
549+ }
550+ . into_response ( ) ) ,
551+ Ok ( SubmitOutcome :: ExistsWithSameParameters ) => Ok ( HttpResponse {
552+ status : StatusCode :: OK ,
553+ message : execution_id. to_string ( ) ,
565554 accept,
566- } ,
567- } )
555+ }
556+ . into_response ( ) ) ,
557+ Err ( err) => Err ( ErrorWrapper ( err, accept) . into ( ) ) ,
558+ }
568559}
569560
570561pub ( crate ) mod components {
@@ -601,7 +592,10 @@ pub(crate) mod components {
601592 state : State < Arc < WebApiState > > ,
602593 ) -> Result < Response , HttpResponse > {
603594 let Some ( wit) = state. component_registry_ro . get_wit ( & digest) else {
604- return Err ( HttpResponse :: not_found ( AcceptHeader :: Text ) ) ;
595+ return Err ( HttpResponse :: not_found (
596+ AcceptHeader :: Text ,
597+ Some ( "component" ) ,
598+ ) ) ;
605599 } ;
606600 Ok ( if let Some ( wit) = wit {
607601 wit. to_string ( ) . into_response ( )
@@ -778,10 +772,14 @@ impl HttpResponse {
778772 }
779773 }
780774
781- fn not_found ( accept : AcceptHeader ) -> Self {
775+ fn not_found ( accept : AcceptHeader , what : Option < & str > ) -> Self {
782776 HttpResponse {
783777 status : StatusCode :: NOT_FOUND ,
784- message : "not found" . to_string ( ) ,
778+ message : if let Some ( what) = what {
779+ format ! ( "{what} not found" )
780+ } else {
781+ "not found" . to_string ( )
782+ } ,
785783 accept,
786784 }
787785 }
@@ -817,7 +815,7 @@ impl From<ErrorWrapper<DbErrorRead>> for HttpResponse {
817815 fn from ( value : ErrorWrapper < DbErrorRead > ) -> Self {
818816 let accept = value. 1 ;
819817 let ( status, message) = match value. 0 {
820- DbErrorRead :: NotFound => return HttpResponse :: not_found ( accept) ,
818+ DbErrorRead :: NotFound => return HttpResponse :: not_found ( accept, None ) ,
821819 DbErrorRead :: Generic ( err) => ( StatusCode :: SERVICE_UNAVAILABLE , err. to_string ( ) ) ,
822820 } ;
823821 HttpResponse {
@@ -831,7 +829,7 @@ impl From<ErrorWrapper<DbErrorWrite>> for HttpResponse {
831829 fn from ( value : ErrorWrapper < DbErrorWrite > ) -> Self {
832830 let accept = value. 1 ;
833831 let ( status, message) = match value. 0 {
834- DbErrorWrite :: NotFound => return HttpResponse :: not_found ( accept) ,
832+ DbErrorWrite :: NotFound => return HttpResponse :: not_found ( accept, None ) ,
835833 DbErrorWrite :: Generic ( err) => ( StatusCode :: SERVICE_UNAVAILABLE , err. to_string ( ) ) ,
836834 DbErrorWrite :: NonRetriable ( err) => ( StatusCode :: INTERNAL_SERVER_ERROR , err. to_string ( ) ) ,
837835 } ;
@@ -842,3 +840,26 @@ impl From<ErrorWrapper<DbErrorWrite>> for HttpResponse {
842840 }
843841 }
844842}
843+ impl From < ErrorWrapper < SubmitError > > for HttpResponse {
844+ fn from ( value : ErrorWrapper < SubmitError > ) -> Self {
845+ let accept = value. 1 ;
846+ match value. 0 {
847+ err @ SubmitError :: Conflict => HttpResponse {
848+ status : StatusCode :: CONFLICT ,
849+ message : err. to_string ( ) ,
850+ accept,
851+ } ,
852+ SubmitError :: FunctionNotFound => HttpResponse :: not_found ( accept, Some ( "ffqn" ) ) ,
853+ SubmitError :: DbErrorWrite ( db_error_write) => {
854+ HttpResponse :: from ( ErrorWrapper ( db_error_write, accept) )
855+ }
856+ err @ ( SubmitError :: ExecutionIdMustBeTopLevel | SubmitError :: ParamsInvalid ( _) ) => {
857+ HttpResponse {
858+ status : StatusCode :: BAD_REQUEST ,
859+ message : err. to_string ( ) ,
860+ accept,
861+ }
862+ }
863+ }
864+ }
865+ }
0 commit comments