Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (a *jobCancelEndpoint) Execute(ctx context.Context, req *jobCancelRequest)
if errors.Is(err, river.ErrNotFound) {
return nil, apierror.NewNotFoundJob(jobID)
}
return nil, fmt.Errorf("error canceling jobs: %w", err)
return nil, err
}
updatedJobs[jobID] = job
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/riverqueue/riverui
go 1.22

require (
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
github.com/riverqueue/river v0.7.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa h1:s+4MhCQ6YrzisK6hFJUX53drDT4UsSW3DEhKn0ifuHw=
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438 h1:Dj0L5fhJ9F82ZJyVOmBx6msDp/kfd1t9GRfny/mfJA0=
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA=
Expand Down
15 changes: 15 additions & 0 deletions internal/apiendpoint/api_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
"net/http"
"time"

"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"

"github.com/riverqueue/riverui/internal/apierror"
)

Expand Down Expand Up @@ -139,6 +142,18 @@ func executeAPIEndpoint[TReq any, TResp any](w http.ResponseWriter, r *http.Requ
return nil
}()
if err != nil {
// Convert certain types of Postgres errors into something more
// user-friendly than an internal server error.
var pgErr *pgconn.PgError
if errors.As(err, &pgErr) {
if pgErr.Code == pgerrcode.InsufficientPrivilege {
err = apierror.WithInternalError(
apierror.NewBadRequest("Insufficient database privilege to perform this operation."),
err,
)
}
}

var apiErr apierror.Interface
if errors.As(err, &apiErr) {
logAttrs := []any{
Expand Down
21 changes: 21 additions & 0 deletions internal/apiendpoint/api_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"testing"
"time"

"github.com/jackc/pgerrcode"
"github.com/jackc/pgx/v5/pgconn"
"github.com/stretchr/testify/require"

"github.com/riverqueue/riverui/internal/apierror"
Expand Down Expand Up @@ -99,6 +102,18 @@ func TestMountAndServe(t *testing.T) {
requireStatusAndJSONResponse(t, http.StatusBadRequest, &apierror.APIError{Message: "Missing message value."}, bundle.recorder)
})

t.Run("InterpretedPostgresError", func(t *testing.T) {
t.Parallel()

mux, bundle := setup(t)

req := httptest.NewRequest(http.MethodPost, "/api/post-endpoint",
bytes.NewBuffer(mustMarshalJSON(t, &postRequest{MakePostgresError: true})))
mux.ServeHTTP(bundle.recorder, req)

requireStatusAndJSONResponse(t, http.StatusBadRequest, &apierror.APIError{Message: "Insufficient database privilege to perform this operation."}, bundle.recorder)
})

t.Run("Timeout", func(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -219,6 +234,7 @@ func (*postEndpoint) Meta() *EndpointMeta {

type postRequest struct {
MakeInternalError bool `json:"make_internal_error"`
MakePostgresError bool `json:"make_postgres_error"`
Message string `json:"message"`
}

Expand All @@ -231,6 +247,11 @@ func (a *postEndpoint) Execute(_ context.Context, req *postRequest) (*postRespon
return nil, errors.New("an internal error occurred")
}

if req.MakePostgresError {
// Wrap the error to make it more realistic.
return nil, fmt.Errorf("error runnning Postgres query: %w", &pgconn.PgError{Code: pgerrcode.InsufficientPrivilege})
}

if req.Message == "" {
return nil, apierror.NewBadRequest("Missing message value.")
}
Expand Down