diff --git a/.github/workflows/prjob_tests.yml b/.github/workflows/prjob_tests.yml index 128f45614..37e30b96e 100644 --- a/.github/workflows/prjob_tests.yml +++ b/.github/workflows/prjob_tests.yml @@ -39,39 +39,7 @@ jobs: run: go get golang.org/x/tools/cmd/cover - name: Tests - continue-on-error: true - run: | - if ! sudo V2_BOT_URL=http://localhost:3005 V2_BOT_TOKEN=xyzxyzxyz go test ./... -failfast -race -v -coverprofile=coverage.out > test_output.log 2>&1; then - echo "::set-output name=test_status::failed" - fi - ./cover-check.sh coverage.out 8.4 - - - name: Upload test log - if: ${{steps.tests.outputs.test_status}} == 'failed' - uses: actions/upload-artifact@v3 - with: - name: test-logs - path: test_output.log - - - name: Parse test log - if: ${{steps.tests.outputs.test_status}} == 'failed' - run: | - cat test_output.log - if grep -q "\-\-\- FAIL" test_output.log; then - echo "Errors found in the logs." - # Write a loop here to get all the errors - # Send a curl request to jarvis use the env vars to set the url and token - - #curl -X POST \ - #-H "Content-Type: application/json" \ - #-H "Authorization: Bearer ${{ secrets.JARVIS_API_TOKEN }}" \ - #-d '{"log": ""}' \ - #${{ secrets.JARVIS_URL }}/endpoint - exit 1 - else - echo "No errors found in the logs." - fi - + run: sudo V2_BOT_URL=http://localhost:3005 V2_BOT_TOKEN=xyzxyzxyz go test ./... -race -v -coverprofile=coverage.out && ./cover-check.sh coverage.out 8.4 - name: Droping DB with docker compose run: docker compose -f ./docker/testdb-docker-compose.yml -p test_db down diff --git a/handlers/auth_test.go b/handlers/auth_test.go index 2528e4688..46b73bd05 100644 --- a/handlers/auth_test.go +++ b/handlers/auth_test.go @@ -39,7 +39,7 @@ func TestGetAdminPubkeys(t *testing.T) { handler.ServeHTTP(rr, req) - if status := rr.Code; status != rr.Status { + if status := rr.Code; status != http.StatusOK { t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) } diff --git a/handlers/bounty_test.go b/handlers/bounty_test.go index 9e1ff1d3b..ee8567ef8 100644 --- a/handlers/bounty_test.go +++ b/handlers/bounty_test.go @@ -613,7 +613,6 @@ func TestDeleteBounty(t *testing.T) { } handler.ServeHTTP(rr, req) - //Check that this fails assert.Equal(t, http.StatusInternalServerError, rr.Code) }) diff --git a/routes/bounty.go b/routes/bounty.go index 0974ae849..ea4eb4c6e 100644 --- a/routes/bounty.go +++ b/routes/bounty.go @@ -7,15 +7,11 @@ import ( "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" - "github.com/stakwork/sphinx-tribes/utils" ) func BountyRoutes() chi.Router { r := chi.NewRouter() bountyHandler := handlers.NewBountyHandler(http.DefaultClient, db.DB) - - r.Use(utils.ErrorHandler) - r.Group(func(r chi.Router) { r.Get("/all", bountyHandler.GetAllBounties) diff --git a/routes/index.go b/routes/index.go index 8da500084..de8b21095 100644 --- a/routes/index.go +++ b/routes/index.go @@ -140,11 +140,36 @@ func getFromAuth(path string) (*extractResponse, error) { }, nil } +// Middleware to handle InternalServerError +func internalServerErrorHandler(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + rr := &responseRecorder{ResponseWriter: w, statusCode: http.StatusOK} + next.ServeHTTP(rr, r) + + if rr.statusCode == http.StatusInternalServerError { + fmt.Printf("Internal Server Error: %s %s\n", r.Method, r.URL.Path) + http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) + } + }) +} + +// Custom ResponseWriter to capture status codes +type responseRecorder struct { + http.ResponseWriter + statusCode int +} + +func (rr *responseRecorder) WriteHeader(code int) { + rr.statusCode = code + rr.ResponseWriter.WriteHeader(code) +} + func initChi() *chi.Mux { r := chi.NewRouter() r.Use(middleware.RequestID) r.Use(middleware.Logger) r.Use(middleware.Recoverer) + r.Use(internalServerErrorHandler) cors := cors.New(cors.Options{ AllowedOrigins: []string{"*"}, AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"}, diff --git a/routes/ticket_routes.go b/routes/ticket_routes.go index 34ebfb8ea..a45cd46c0 100644 --- a/routes/ticket_routes.go +++ b/routes/ticket_routes.go @@ -7,15 +7,12 @@ import ( "github.com/stakwork/sphinx-tribes/auth" "github.com/stakwork/sphinx-tribes/db" "github.com/stakwork/sphinx-tribes/handlers" - "github.com/stakwork/sphinx-tribes/utils" ) func TicketRoutes() chi.Router { r := chi.NewRouter() ticketHandler := handlers.NewTicketHandler(http.DefaultClient, db.DB) - r.Use(utils.ErrorHandler) - r.Group(func(r chi.Router) { r.Get("/{uuid}", ticketHandler.GetTicket) r.Post("/review", ticketHandler.ProcessTicketReview) diff --git a/utils/error_handler.go b/utils/error_handler.go deleted file mode 100644 index f62195d47..000000000 --- a/utils/error_handler.go +++ /dev/null @@ -1,54 +0,0 @@ -package utils - -import ( - "database/sql" - "encoding/json" - "errors" - "fmt" - - "net/http" - ) - -type customError struct { - error - StatusCode int -} - -func ErrorHandler(next http.Handler) http.Handler { - return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - defer func() { - if err := recover(); err != nil { - // Add logic here to then send to jarvis with the correct values - http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusNotFound) - } - }() - - ww := &responseWriterWrapper{ResponseWriter: w} - next.ServeHTTP(ww, r) - - if ww.error != nil { - //statusCode := http.StatusInternalServerError - statusCode := http.StatusNotFound - if errors.Is(ww.error, sql.ErrNoRows) { - statusCode = http.StatusNotFound - } else if err, ok := ww.error.(*customError); ok { - statusCode = err.StatusCode - } - - w.WriteHeader(statusCode) - json.NewEncoder(w).Encode(map[string]string{"error": ww.error.Error()}) - } - }) -} - -type responseWriterWrapper struct { - http.ResponseWriter - error error -} - -func (w *responseWriterWrapper) WriteHeader(statusCode int) { - if statusCode >= http.StatusBadRequest { - w.error = fmt.Errorf("HTTP %d: %s", statusCode, http.StatusText(statusCode)) - } - w.ResponseWriter.WriteHeader(statusCode) -}