Skip to content

Commit

Permalink
ref: Change DSN ProjectID to be a string (#420)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilogorek committed Mar 10, 2022
1 parent 8c2dbee commit b1bb14d
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 7 additions & 6 deletions dsn.go
Expand Up @@ -45,7 +45,7 @@ type Dsn struct {
host string
port int
path string
projectID int
projectID string
}

// NewDsn creates a Dsn by parsing rawURL. Most users will never call this
Expand Down Expand Up @@ -104,9 +104,10 @@ func NewDsn(rawURL string) (*Dsn, error) {
return nil, &DsnParseError{"empty project id"}
}
pathSegments := strings.Split(parsedURL.Path[1:], "/")
projectID, err := strconv.Atoi(pathSegments[len(pathSegments)-1])
if err != nil {
return nil, &DsnParseError{"invalid project id"}
projectID := pathSegments[len(pathSegments)-1]

if projectID == "" {
return nil, &DsnParseError{"empty project id"}
}

// Path
Expand Down Expand Up @@ -140,7 +141,7 @@ func (dsn Dsn) String() string {
if dsn.path != "" {
url += dsn.path
}
url += fmt.Sprintf("/%d", dsn.projectID)
url += fmt.Sprintf("/%s", dsn.projectID)
return url
}

Expand All @@ -165,7 +166,7 @@ func (dsn Dsn) getAPIURL(s string) *url.URL {
if dsn.path != "" {
rawURL += dsn.path
}
rawURL += fmt.Sprintf("/api/%d/%s/", dsn.projectID, s)
rawURL += fmt.Sprintf("/api/%s/%s/", dsn.projectID, s)
parsedURL, _ := url.Parse(rawURL)
return parsedURL
}
Expand Down
9 changes: 4 additions & 5 deletions dsn_test.go
Expand Up @@ -26,7 +26,7 @@ var dsnTests = map[string]DsnTest{
host: "domain",
port: 8888,
path: "/foo/bar",
projectID: 42,
projectID: "42",
},
url: "https://domain:8888/foo/bar/api/42/store/",
envURL: "https://domain:8888/foo/bar/api/42/envelope/",
Expand All @@ -38,7 +38,7 @@ var dsnTests = map[string]DsnTest{
publicKey: "public",
host: "domain",
port: 443,
projectID: 42,
projectID: "42",
},
url: "https://domain/api/42/store/",
envURL: "https://domain/api/42/envelope/",
Expand All @@ -50,7 +50,7 @@ var dsnTests = map[string]DsnTest{
publicKey: "public",
host: "domain",
port: 80,
projectID: 42,
projectID: "42",
},
url: "http://domain/api/42/store/",
envURL: "http://domain/api/42/envelope/",
Expand Down Expand Up @@ -100,9 +100,8 @@ var invalidDsnTests = map[string]invalidDsnTest{
"NoProjectID2": {"https://public:secret@domain:8888", "empty project id"},
"BadURL": {"!@#$%^&*()", "invalid url"},
"BadScheme": {"ftp://public:secret@domain:8888/1", "invalid scheme"},
"BadProjectID": {"https://public:secret@domain:8888/wbvdf7^W#$", "invalid project id"},
"BadPort": {"https://public:secret@domain:wat/42", "invalid port"},
"TrailingSlash": {"https://public:secret@domain:8888/42/", "invalid project id"},
"TrailingSlash": {"https://public:secret@domain:8888/42/", "empty project id"},
}

//nolint: scopelint // false positive https://github.com/kyoh86/scopelint/issues/4
Expand Down
4 changes: 2 additions & 2 deletions transport.go
Expand Up @@ -304,7 +304,7 @@ func (t *HTTPTransport) SendEvent(event *Event) {
eventType = fmt.Sprintf("%s event", event.Level)
}
Logger.Printf(
"Sending %s [%s] to %s project: %d",
"Sending %s [%s] to %s project: %s",
eventType,
event.EventID,
t.dsn.host,
Expand Down Expand Up @@ -511,7 +511,7 @@ func (t *HTTPSyncTransport) SendEvent(event *Event) {
eventType = fmt.Sprintf("%s event", event.Level)
}
Logger.Printf(
"Sending %s [%s] to %s project: %d",
"Sending %s [%s] to %s project: %s",
eventType,
event.EventID,
t.dsn.host,
Expand Down

0 comments on commit b1bb14d

Please sign in to comment.