-
Notifications
You must be signed in to change notification settings - Fork 0
/
codex_upload.go
101 lines (86 loc) · 2.49 KB
/
codex_upload.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package api
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
type UploadCodexRequest struct {
// required
// the codex category that the uploaded codex should belong to
CodexCategoryId string `json:"codexCategoryId"`
// required
// the files that comprise the codex bundle
Files []FileRef `json:"-"`
// optional
// if specified, replace this codex instead of uploading a new codex
CodexId string `json:"replaceCodexId,omitempty"`
KernelOptions KernelOptions `json:"kernelOptions"`
}
type KernelOptions struct {
SystemPackages []string `json:"systemPackages,omitempty"`
}
type UploadCodexResponse struct {
CodexId string `json:"codexId"`
}
type CodexParseError struct {
// The type of the error
Error string `json:"error"`
Message string `json:"message"`
SourcePosition string `json:"sourcePosition"`
SourceInfo struct {
SourceContext struct {
Lines []string `json:"lines"`
} `json:"sourceContext"`
} `json:"sourceInfo"`
}
type CodexParseFailedError struct {
Errors []CodexParseError `json:"errors"`
}
func (e *CodexParseFailedError) Error() string {
return fmt.Sprintf("failed to parse codex: %d errors occurred", len(e.Errors))
}
var _ error = (*CodexParseFailedError)(nil)
func (c *Client) UploadCodex(
r *UploadCodexRequest,
) (*UploadCodexResponse, *CodexParseFailedError, error) {
res, err := c.postMultipart(&multipartRequest{
route: "author/upload-codex",
payload: r,
files: r.Files,
})
if err != nil {
return nil, nil, err
}
statusError, err := res.StatusError()
if err != nil {
return nil, nil, err
}
if statusError != nil {
switch statusError.error.Error {
case "MySTParseFailedErr", "CodexASTParseFailedErr":
var parseError CodexParseFailedError
if err := json.Unmarshal(statusError.error.Details, &parseError); err != nil {
return nil, nil, errors.Wrap(err, "failed to unmarshal codex parse error details")
}
return nil, &parseError, nil
case "ErrUnauthenticated":
log.WithError(statusError).Debug("got ErrUnauthenticated")
return nil, nil, errors.New("You are not logged in (try running `pbauthor auth login`)")
case "":
return nil, nil, errors.Errorf("API returned an unknown error")
default:
return nil, nil, errors.Errorf(
"API returned an error: %s: %s",
statusError.error.Error,
statusError.error.Message,
)
}
}
resp := &UploadCodexResponse{}
err = res.UnmarshalJson(resp)
if err != nil {
return nil, nil, err
}
return resp, nil, nil
}