Skip to content

Commit

Permalink
fix: improve error messages of 422 errors (jsdelivr#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKolarik authored Sep 24, 2024
1 parent acfb26f commit be19e61
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ func silenceUsageOnCreateMeasurementError(err error) bool {
e, ok := err.(*globalping.MeasurementError)
if ok {
switch e.Code {
case http.StatusBadRequest, http.StatusUnprocessableEntity:
case http.StatusBadRequest:
return false
default:
return true
Expand Down
5 changes: 3 additions & 2 deletions globalping/measurements.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (c *client) CreateMeasurement(measurement *MeasurementCreate) (*Measurement
}
err := data.Error
err.Code = resp.StatusCode

if resp.StatusCode == http.StatusBadRequest {
resErr := ""
for _, v := range data.Error.Params {
Expand All @@ -68,7 +69,7 @@ func (c *client) CreateMeasurement(measurement *MeasurementCreate) (*Measurement
if len(resErr) > 0 {
resErr = resErr[:len(resErr)-1]
}
err.Message = fmt.Sprintf("invalid parameters\n%s", resErr)
err.Message = fmt.Sprintf("invalid parameters:\n%s", resErr)
return nil, err
}

Expand All @@ -82,7 +83,7 @@ func (c *client) CreateMeasurement(measurement *MeasurementCreate) (*Measurement
}

if resp.StatusCode == http.StatusUnprocessableEntity {
err.Message = "no suitable probes found - please choose a different location"
err.Message = fmt.Sprintf("%s - please try a different location", utils.TextFromSentence(err.Message))
return nil, err
}

Expand Down
4 changes: 2 additions & 2 deletions globalping/measurements_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ func Test_CreateMeasurement_NoProbes(t *testing.T) {
assert.Equal(t, &MeasurementError{
Code: 422,
Type: "no_probes_found",
Message: "no suitable probes found - please choose a different location",
Message: "no suitable probes found - please try a different location",
}, err)
}

Expand All @@ -206,7 +206,7 @@ func Test_CreateMeasurement_Validation(t *testing.T) {
assert.Equal(t, &MeasurementError{
Code: 400,
Type: "validation_error",
Message: `invalid parameters
Message: `invalid parameters:
- "target" does not match any of the allowed types`,
Params: map[string]interface{}{
"target": "\"target\" does not match any of the allowed types",
Expand Down
28 changes: 27 additions & 1 deletion utils/text.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,36 @@
package utils

import "fmt"
import (
"fmt"
"unicode"
)

func LowercaseMessage(message string) string {
if len(message) >= 2 {
firstChar := rune(message[0])
secondChar := rune(message[1])

if !unicode.IsLower(firstChar) && unicode.IsLower(secondChar) {
return string(unicode.ToLower(firstChar)) + message[1:]
}
}
return message
}

func Pluralize(count int64, singular string) string {
if count == 1 {
return fmt.Sprintf("%d %s", count, singular)
}
return fmt.Sprintf("%d %ss", count, singular)
}

func RemoveTrailingPeriod(message string) string {
if len(message) > 0 && message[len(message)-1] == '.' {
return message[:len(message)-1]
}
return message
}

func TextFromSentence(message string) string {
return RemoveTrailingPeriod(LowercaseMessage(message))
}

0 comments on commit be19e61

Please sign in to comment.