Skip to content

Commit 196762d

Browse files
committed
TUN-3527: More specific error for invalid YAML/JSON
1 parent 350a6f2 commit 196762d

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

cmd/cloudflared/config/configuration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,7 @@ func ReadConfigFile(c *cli.Context, log logger.Service) (*configFileSettings, er
391391
}
392392
defer file.Close()
393393
if err := yaml.NewDecoder(file).Decode(&configuration); err != nil {
394-
return nil, errors.Wrap(err, "error parsing config file at "+configFile)
394+
return nil, errors.Wrap(err, "error parsing YAML in config file at "+configFile)
395395
}
396396
configuration.sourceFile = configFile
397397
return &configuration, nil

cmd/cloudflared/config/manager.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package config
22

33
import (
4-
"errors"
54
"os"
65

76
"github.com/cloudflare/cloudflared/logger"
87
"github.com/cloudflare/cloudflared/watcher"
8+
"github.com/pkg/errors"
99
"gopkg.in/yaml.v2"
1010
)
1111

@@ -80,7 +80,7 @@ func readConfigFromPath(configPath string) (Root, error) {
8080

8181
var config Root
8282
if err := yaml.NewDecoder(file).Decode(&config); err != nil {
83-
return Root{}, err
83+
return Root{}, errors.Wrap(err, "error parsing YAML in config file at "+configPath)
8484
}
8585

8686
return config, nil

cmd/cloudflared/tunnel/subcommand_context.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ import (
2020
"github.com/cloudflare/cloudflared/tunnelstore"
2121
)
2222

23+
type errInvalidJSONCredential struct {
24+
err error
25+
path string
26+
}
27+
28+
func (e errInvalidJSONCredential) Error() string {
29+
return "Invalid JSON when parsing tunnel credentials file"
30+
}
31+
2332
// subcommandContext carries structs shared between subcommands, to reduce number of arguments needed to
2433
// pass between subcommands, and make sure they are only initialized once
2534
type subcommandContext struct {
@@ -111,7 +120,7 @@ func (sc *subcommandContext) readTunnelCredentials(tunnelID uuid.UUID) (*pogs.Tu
111120

112121
var auth pogs.TunnelAuth
113122
if err = json.Unmarshal(body, &auth); err != nil {
114-
return nil, err
123+
return nil, errInvalidJSONCredential{path: filePath, err: err}
115124
}
116125
return &auth, nil
117126
}
@@ -244,6 +253,10 @@ func (sc *subcommandContext) delete(tunnelIDs []uuid.UUID) error {
244253
func (sc *subcommandContext) run(tunnelID uuid.UUID) error {
245254
credentials, err := sc.readTunnelCredentials(tunnelID)
246255
if err != nil {
256+
if e, ok := err.(errInvalidJSONCredential); ok {
257+
sc.logger.Errorf("The credentials file at %s contained invalid JSON. This is probably caused by passing the wrong filepath. Reminder: the credentials file is a .json file created via `cloudflared tunnel create`.", e.path)
258+
sc.logger.Errorf("Invalid JSON when parsing credentials file: %s", e.err.Error())
259+
}
247260
return err
248261
}
249262

0 commit comments

Comments
 (0)