@@ -37,8 +37,10 @@ use reqwest::{Client, Method, StatusCode, Url};
3737use tokio:: sync:: OnceCell ;
3838use typed_builder:: TypedBuilder ;
3939
40- use crate :: client:: {
41- HttpClient , deserialize_catalog_response, deserialize_unexpected_catalog_error,
40+ use crate :: client:: { HttpClient , deserialize_catalog_response, handle_error_response} ;
41+ use crate :: error_handlers:: {
42+ DefaultErrorHandler , DropNamespaceErrorHandler , NamespaceErrorHandler , TableCommitHandler ,
43+ TableErrorHandler ,
4244} ;
4345use crate :: types:: {
4446 CatalogConfig , CommitTableRequest , CommitTableResponse , CreateNamespaceRequest ,
@@ -378,7 +380,7 @@ impl RestCatalog {
378380
379381 match http_response. status ( ) {
380382 StatusCode :: OK => deserialize_catalog_response ( http_response) . await ,
381- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
383+ _ => Err ( handle_error_response ( http_response, & DefaultErrorHandler ) . await ) ,
382384 }
383385 }
384386
@@ -473,13 +475,7 @@ impl Catalog for RestCatalog {
473475 None => break ,
474476 }
475477 }
476- StatusCode :: NOT_FOUND => {
477- return Err ( Error :: new (
478- ErrorKind :: Unexpected ,
479- "The parent parameter of the namespace provided does not exist" ,
480- ) ) ;
481- }
482- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
478+ _ => return Err ( handle_error_response ( http_response, & NamespaceErrorHandler ) . await ) ,
483479 }
484480 }
485481
@@ -510,11 +506,7 @@ impl Catalog for RestCatalog {
510506 deserialize_catalog_response :: < NamespaceResponse > ( http_response) . await ?;
511507 Ok ( Namespace :: from ( response) )
512508 }
513- StatusCode :: CONFLICT => Err ( Error :: new (
514- ErrorKind :: Unexpected ,
515- "Tried to create a namespace that already exists" ,
516- ) ) ,
517- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
509+ _ => Err ( handle_error_response ( http_response, & NamespaceErrorHandler ) . await ) ,
518510 }
519511 }
520512
@@ -534,11 +526,7 @@ impl Catalog for RestCatalog {
534526 deserialize_catalog_response :: < NamespaceResponse > ( http_response) . await ?;
535527 Ok ( Namespace :: from ( response) )
536528 }
537- StatusCode :: NOT_FOUND => Err ( Error :: new (
538- ErrorKind :: Unexpected ,
539- "Tried to get a namespace that does not exist" ,
540- ) ) ,
541- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
529+ _ => Err ( handle_error_response ( http_response, & NamespaceErrorHandler ) . await ) ,
542530 }
543531 }
544532
@@ -555,7 +543,7 @@ impl Catalog for RestCatalog {
555543 match http_response. status ( ) {
556544 StatusCode :: NO_CONTENT | StatusCode :: OK => Ok ( true ) ,
557545 StatusCode :: NOT_FOUND => Ok ( false ) ,
558- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
546+ _ => Err ( handle_error_response ( http_response, & NamespaceErrorHandler ) . await ) ,
559547 }
560548 }
561549
@@ -582,11 +570,7 @@ impl Catalog for RestCatalog {
582570
583571 match http_response. status ( ) {
584572 StatusCode :: NO_CONTENT | StatusCode :: OK => Ok ( ( ) ) ,
585- StatusCode :: NOT_FOUND => Err ( Error :: new (
586- ErrorKind :: Unexpected ,
587- "Tried to drop a namespace that does not exist" ,
588- ) ) ,
589- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
573+ _ => Err ( handle_error_response ( http_response, & DropNamespaceErrorHandler ) . await ) ,
590574 }
591575 }
592576
@@ -617,13 +601,7 @@ impl Catalog for RestCatalog {
617601 None => break ,
618602 }
619603 }
620- StatusCode :: NOT_FOUND => {
621- return Err ( Error :: new (
622- ErrorKind :: Unexpected ,
623- "Tried to list tables of a namespace that does not exist" ,
624- ) ) ;
625- }
626- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
604+ _ => return Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
627605 }
628606 }
629607
@@ -665,19 +643,7 @@ impl Catalog for RestCatalog {
665643 StatusCode :: OK => {
666644 deserialize_catalog_response :: < LoadTableResult > ( http_response) . await ?
667645 }
668- StatusCode :: NOT_FOUND => {
669- return Err ( Error :: new (
670- ErrorKind :: Unexpected ,
671- "Tried to create a table under a namespace that does not exist" ,
672- ) ) ;
673- }
674- StatusCode :: CONFLICT => {
675- return Err ( Error :: new (
676- ErrorKind :: Unexpected ,
677- "The table already exists" ,
678- ) ) ;
679- }
680- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
646+ _ => return Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
681647 } ;
682648
683649 let metadata_location = response. metadata_location . as_ref ( ) . ok_or ( Error :: new (
@@ -726,13 +692,7 @@ impl Catalog for RestCatalog {
726692 StatusCode :: OK | StatusCode :: NOT_MODIFIED => {
727693 deserialize_catalog_response :: < LoadTableResult > ( http_response) . await ?
728694 }
729- StatusCode :: NOT_FOUND => {
730- return Err ( Error :: new (
731- ErrorKind :: Unexpected ,
732- "Tried to load a table that does not exist" ,
733- ) ) ;
734- }
735- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
695+ _ => return Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
736696 } ;
737697
738698 let config = response
@@ -770,11 +730,7 @@ impl Catalog for RestCatalog {
770730
771731 match http_response. status ( ) {
772732 StatusCode :: NO_CONTENT | StatusCode :: OK => Ok ( ( ) ) ,
773- StatusCode :: NOT_FOUND => Err ( Error :: new (
774- ErrorKind :: Unexpected ,
775- "Tried to drop a table that does not exist" ,
776- ) ) ,
777- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
733+ _ => Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
778734 }
779735 }
780736
@@ -792,7 +748,7 @@ impl Catalog for RestCatalog {
792748 match http_response. status ( ) {
793749 StatusCode :: NO_CONTENT | StatusCode :: OK => Ok ( true ) ,
794750 StatusCode :: NOT_FOUND => Ok ( false ) ,
795- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
751+ _ => Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
796752 }
797753 }
798754
@@ -813,15 +769,7 @@ impl Catalog for RestCatalog {
813769
814770 match http_response. status ( ) {
815771 StatusCode :: NO_CONTENT | StatusCode :: OK => Ok ( ( ) ) ,
816- StatusCode :: NOT_FOUND => Err ( Error :: new (
817- ErrorKind :: Unexpected ,
818- "Tried to rename a table that does not exist (is the namespace correct?)" ,
819- ) ) ,
820- StatusCode :: CONFLICT => Err ( Error :: new (
821- ErrorKind :: Unexpected ,
822- "Tried to rename a table to a name that already exists" ,
823- ) ) ,
824- _ => Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
772+ _ => Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
825773 }
826774 }
827775
@@ -853,19 +801,7 @@ impl Catalog for RestCatalog {
853801 StatusCode :: OK => {
854802 deserialize_catalog_response :: < LoadTableResult > ( http_response) . await ?
855803 }
856- StatusCode :: NOT_FOUND => {
857- return Err ( Error :: new (
858- ErrorKind :: NamespaceNotFound ,
859- "The namespace specified does not exist." ,
860- ) ) ;
861- }
862- StatusCode :: CONFLICT => {
863- return Err ( Error :: new (
864- ErrorKind :: TableAlreadyExists ,
865- "The given table already exists." ,
866- ) ) ;
867- }
868- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
804+ _ => return Err ( handle_error_response ( http_response, & TableErrorHandler ) . await ) ,
869805 } ;
870806
871807 let metadata_location = response. metadata_location . as_ref ( ) . ok_or ( Error :: new (
@@ -903,38 +839,7 @@ impl Catalog for RestCatalog {
903839
904840 let response: CommitTableResponse = match http_response. status ( ) {
905841 StatusCode :: OK => deserialize_catalog_response ( http_response) . await ?,
906- StatusCode :: NOT_FOUND => {
907- return Err ( Error :: new (
908- ErrorKind :: TableNotFound ,
909- "Tried to update a table that does not exist" ,
910- ) ) ;
911- }
912- StatusCode :: CONFLICT => {
913- return Err ( Error :: new (
914- ErrorKind :: CatalogCommitConflicts ,
915- "CatalogCommitConflicts, one or more requirements failed. The client may retry." ,
916- )
917- . with_retryable ( true ) ) ;
918- }
919- StatusCode :: INTERNAL_SERVER_ERROR => {
920- return Err ( Error :: new (
921- ErrorKind :: Unexpected ,
922- "An unknown server-side problem occurred; the commit state is unknown." ,
923- ) ) ;
924- }
925- StatusCode :: BAD_GATEWAY => {
926- return Err ( Error :: new (
927- ErrorKind :: Unexpected ,
928- "A gateway or proxy received an invalid response from the upstream server; the commit state is unknown." ,
929- ) ) ;
930- }
931- StatusCode :: GATEWAY_TIMEOUT => {
932- return Err ( Error :: new (
933- ErrorKind :: Unexpected ,
934- "A server-side gateway timeout occurred; the commit state is unknown." ,
935- ) ) ;
936- }
937- _ => return Err ( deserialize_unexpected_catalog_error ( http_response) . await ) ,
842+ _ => return Err ( handle_error_response ( http_response, & TableCommitHandler ) . await ) ,
938843 } ;
939844
940845 let file_io = self
0 commit comments