Skip to content

Commit 76704ff

Browse files
Hasura: error parsing
1 parent 40ab766 commit 76704ff

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

hasura/api.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package hasura
22

33
import (
44
"bytes"
5-
"io/ioutil"
65
"net/http"
76
"net/url"
87
"path"
@@ -83,11 +82,11 @@ func (api *API) post(endpoint string, args map[string]string, body interface{},
8382
defer resp.Body.Close()
8483

8584
if resp.StatusCode != http.StatusOK {
86-
bodyBytes, err := ioutil.ReadAll(resp.Body)
87-
if err != nil {
85+
var apiError APIError
86+
if err := json.NewDecoder(resp.Body).Decode(&apiError); err != nil {
8887
return errors.Wrap(err, "Hasura's response decoding error")
8988
}
90-
return errors.Errorf("Invalid status code: %s %s", resp.Status, string(bodyBytes))
89+
return apiError
9190
}
9291

9392
if output == nil {
@@ -141,10 +140,10 @@ func (api *API) ReplaceMetadata(data *Metadata) error {
141140
// TrackTable -
142141
func (api *API) TrackTable(schema, name string) error {
143142
req := request{
144-
Type: "track_table",
143+
Type: "pg_track_table",
145144
Args: map[string]string{
146-
"schema": schema,
147-
"name": name,
145+
"source": schema,
146+
"table": name,
148147
},
149148
}
150149
return api.post("/v1/query", nil, req, nil)

hasura/error.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package hasura
2+
3+
import "strings"
4+
5+
// APIError -
6+
type APIError struct {
7+
Path string `json:"path"`
8+
Text string `json:"error"`
9+
Code string `json:"code"`
10+
}
11+
12+
// Error -
13+
func (e APIError) Error() string {
14+
var builder strings.Builder
15+
builder.WriteString("hasura api error")
16+
if e.Path != "" {
17+
builder.WriteString(" path=")
18+
builder.WriteString(e.Path)
19+
}
20+
if e.Text != "" {
21+
builder.WriteString(" error=")
22+
builder.WriteString(e.Text)
23+
}
24+
if e.Code != "" {
25+
builder.WriteString(" code=")
26+
builder.WriteString(e.Code)
27+
}
28+
return builder.String()
29+
}
30+
31+
// AlreadyExists -
32+
func (e APIError) AlreadyExists() bool {
33+
return e.Code == "already-exists"
34+
}

hasura/hasura.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ func Create(hasura config.Hasura, cfg config.Database, views []string, models ..
6464
log.Info("Creating REST endpoints...")
6565
for _, query := range metadata.QueryCollections[0].Definition.Queries {
6666
if err := api.CreateRestEndpoint(query.Name, query.Name, query.Name, allowedQueries); err != nil {
67-
return err
67+
if e, ok := err.(APIError); !ok || !e.AlreadyExists() {
68+
return err
69+
}
6870
}
6971
}
7072
}

state/db.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ func OpenConnection(cfg config.Database) (*gorm.DB, error) {
4444
}
4545

4646
return gorm.Open(dialector, &gorm.Config{
47+
SkipDefaultTransaction: true,
48+
PrepareStmt: true,
4749
Logger: logger.New(
4850
log.New(os.Stdout, "\r\n", log.LstdFlags),
4951
logger.Config{

0 commit comments

Comments
 (0)