@@ -772,3 +772,70 @@ func ExampleInfo_DefaultSort() {
772772 // Output: testquest
773773 // testquest
774774}
775+
776+ func TestInfo_HandleErrors (t * testing.T ) {
777+ w := httptest .NewRecorder ()
778+ r := httptest .NewRequest (http .MethodPost , "/" , nil )
779+
780+ inf := Info {
781+ request : r ,
782+ w : w ,
783+ Tx : & sqlx.Tx {},
784+ }
785+
786+ code := http .StatusFailedDependency
787+ inf .HandleErrors (NewErrors (code , errors .New ("test" ), errors .New ("quest" )))
788+
789+ if w .Code != code {
790+ t .Errorf ("incorrect response status code; want: %d, got: %d" , code , w .Code )
791+ }
792+
793+ var alerts tc.Alerts
794+ if err := json .NewDecoder (w .Body ).Decode (& alerts ); err != nil {
795+ t .Fatalf ("couldn't decode response body: %v" , err )
796+ }
797+
798+ if len (alerts .Alerts ) != 1 {
799+ t .Fatalf ("expected exactly one alert; got: %d" , len (alerts .Alerts ))
800+ }
801+ alert := alerts .Alerts [0 ]
802+ if alert .Level != tc .ErrorLevel .String () {
803+ t .Errorf ("Incorrect alert level; want: %s, got: %s" , tc .ErrorLevel , alert .Level )
804+ }
805+ if alert .Text != "test" {
806+ t .Errorf ("Incorrect alert text; want: 'test', got: '%s'" , alert .Text )
807+ }
808+ }
809+
810+ func TestInfo_HandleDBError (t * testing.T ) {
811+ w := httptest .NewRecorder ()
812+ r := httptest .NewRequest (http .MethodPost , "/" , nil )
813+
814+ inf := Info {
815+ request : r ,
816+ w : w ,
817+ Tx : & sqlx.Tx {},
818+ }
819+
820+ inf .HandleDBError (errors .New ("a non-parsable error" ))
821+
822+ if code := http .StatusInternalServerError ; w .Code != code {
823+ t .Errorf ("incorrect response status code; want: %d, got: %d" , code , w .Code )
824+ }
825+
826+ var alerts tc.Alerts
827+ if err := json .NewDecoder (w .Body ).Decode (& alerts ); err != nil {
828+ t .Fatalf ("couldn't decode response body: %v" , err )
829+ }
830+
831+ if len (alerts .Alerts ) != 1 {
832+ t .Fatalf ("expected exactly one alert; got: %d" , len (alerts .Alerts ))
833+ }
834+ alert := alerts .Alerts [0 ]
835+ if alert .Level != tc .ErrorLevel .String () {
836+ t .Errorf ("Incorrect alert level; want: %s, got: %s" , tc .ErrorLevel , alert .Level )
837+ }
838+ if expected := http .StatusText (http .StatusInternalServerError ); alert .Text != expected {
839+ t .Errorf ("Incorrect alert text; want: '%s', got: '%s'" , expected , alert .Text )
840+ }
841+ }
0 commit comments