@@ -738,4 +738,60 @@ test('findOne: .except() - For the reference - Array', function(assert) {
738
738
assert . fail ( "findOne: .except() - For the reference - Array" ) ;
739
739
assert . end ( ) ;
740
740
} ) ;
741
+ } ) ;
742
+ /*!
743
+ * HTTP Error Handling
744
+ * !*/
745
+
746
+ test ( 'findOne: should handle 404 Not Found error' , function ( assert ) {
747
+ const Query = Stack . ContentType ( contentTypes . invalid_type ) . Query ( ) ;
748
+
749
+ Query
750
+ . toJSON ( )
751
+ . findOne ( )
752
+ . then ( function success ( ) {
753
+ assert . fail ( "Expected 404 error but got a successful response." ) ;
754
+ assert . end ( ) ;
755
+ } , function error ( err ) {
756
+ assert . equal ( err . http_code , 404 , 'Should return HTTP status 404.' ) ;
757
+ assert . ok ( err . http_message , 'Error message should be present.' ) ;
758
+ console . error ( "Error:" , err . http_message ) ;
759
+ assert . end ( ) ;
760
+ } ) ;
761
+ } ) ;
762
+
763
+ test ( 'findOne: should handle 401 Unauthorized error' , function ( assert ) {
764
+ Stack . headers = { authorization : 'InvalidAPIKey' } ; // Simulating an invalid API key
765
+ const Query = Stack . ContentType ( contentTypes . source ) . Query ( ) ;
766
+
767
+ Query
768
+ . toJSON ( )
769
+ . findOne ( )
770
+ . then ( function success ( ) {
771
+ assert . fail ( "Expected 401 error but got a successful response." ) ;
772
+ assert . end ( ) ;
773
+ } , function error ( err ) {
774
+ assert . equal ( err . http_code , 401 , 'Should return HTTP status 401.' ) ;
775
+ assert . ok ( err . http_message , 'Error message should be present.' ) ;
776
+ console . error ( "Error:" , err . http_message ) ;
777
+ assert . end ( ) ;
778
+ } ) ;
779
+ } ) ;
780
+
781
+ test ( 'findOne: should handle 500 Internal Server Error' , function ( assert ) {
782
+ const mockStack = Contentstack . Stack ( { ...init . stack , host : 'invalid.host' } ) ; // Simulating a server error
783
+ const Query = mockStack . ContentType ( contentTypes . source ) . Query ( ) ;
784
+
785
+ Query
786
+ . toJSON ( )
787
+ . findOne ( )
788
+ . then ( function success ( ) {
789
+ assert . fail ( "Expected 500 error but got a successful response." ) ;
790
+ assert . end ( ) ;
791
+ } , function error ( err ) {
792
+ assert . equal ( err . http_code , 500 , 'Should return HTTP status 500.' ) ;
793
+ assert . ok ( err . http_message , 'Error message should be present.' ) ;
794
+ console . error ( "Error:" , err . http_message ) ;
795
+ assert . end ( ) ;
796
+ } ) ;
741
797
} ) ;
0 commit comments