Skip to content

Commit bd9919d

Browse files
feat: Abort compiler when rpc fails as unauthenticated (#2887)
* feat: Abort compiler when rpc fails as unauthenticated * test: Bump timeout to 20 minutes * return early --------- Co-authored-by: Kyle Conroy <[email protected]>
1 parent 68d27bf commit bd9919d

File tree

4 files changed

+20
-7
lines changed

4 files changed

+20
-7
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ jobs:
5757
run: go install ./...
5858

5959
- name: test ./...
60-
run: gotestsum --junitfile junit.xml -- --tags=examples ./...
60+
run: gotestsum --junitfile junit.xml -- --tags=examples -timeout 20m ./...
6161
env:
6262
MYSQL_DATABASE: mysql
6363
MYSQL_HOST: localhost

internal/compiler/compile.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/sqlc-dev/sqlc/internal/migrations"
1212
"github.com/sqlc-dev/sqlc/internal/multierr"
1313
"github.com/sqlc-dev/sqlc/internal/opts"
14+
"github.com/sqlc-dev/sqlc/internal/rpc"
1415
"github.com/sqlc-dev/sqlc/internal/source"
1516
"github.com/sqlc-dev/sqlc/internal/sql/ast"
1617
"github.com/sqlc-dev/sqlc/internal/sql/sqlerr"
@@ -88,6 +89,10 @@ func (c *Compiler) parseQueries(o opts.Parser) (*Result, error) {
8889
loc = e.Location
8990
}
9091
merr.Add(filename, src, loc, err)
92+
// If this rpc unauthenticated error bubbles up, then all future parsing/analysis will fail
93+
if errors.Is(err, rpc.ErrUnauthenticated) {
94+
return nil, merr
95+
}
9196
continue
9297
}
9398
queryName := query.Metadata.Name

internal/rpc/errors.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package rpc
2+
3+
import (
4+
"google.golang.org/grpc/codes"
5+
"google.golang.org/grpc/status"
6+
)
7+
8+
const errMessageUnauthenticated = `rpc authentication failed
9+
10+
You may be using a sqlc auth token that was created for a different project,
11+
or your auth token may have expired.`
12+
13+
var ErrUnauthenticated = status.New(codes.Unauthenticated, errMessageUnauthenticated).Err()

internal/rpc/interceptor.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,14 @@ import (
88
"google.golang.org/grpc/status"
99
)
1010

11-
const errMessageUnauthenticated = `rpc authentication failed
12-
13-
You may be using a sqlc auth token that was created for a different project,
14-
or your auth token may have expired.`
15-
1611
func UnaryInterceptor(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
1712
err := invoker(ctx, method, req, reply, cc, opts...)
1813

1914
switch status.Convert(err).Code() {
2015
case codes.OK:
2116
return nil
2217
case codes.Unauthenticated:
23-
return status.New(codes.Unauthenticated, errMessageUnauthenticated).Err()
18+
return ErrUnauthenticated
2419
default:
2520
return err
2621
}

0 commit comments

Comments
 (0)