Пример #1
0
func parseError(err error) (c int, e boom.Error) {
	switch err.(type) {
	case geocode.LocationNotFound:
		c = 404
		e = boom.NewError(err.Error())
	default:
		c = http.StatusInternalServerError
		e = boom.NewError("An error occurred.")
	}

	return c, e
}
Пример #2
0
func parseError(err error) (c int, e boom.Error) {
	switch t := err.(type) {
	case repository.NoRows:
		c = 404
		e = boom.NewError("The requested resource does not exist")
	case boom.Error:
		c = 400
		e = t
	default:
		c = http.StatusInternalServerError
		e = boom.NewError("An error occurred.")
	}

	return c, e
}
Пример #3
0
func (c Validator) IsCriteriaValid(d decode.Criteria, ctx validation.Context) error {
	e := boom.NewError("Query error. Please fix the errors before continuing.")

	for _, filter := range d.Filters {
		switch filter.Field {
		case "Bounds":
			if len(strings.Split(filter.Value, ",")) != 4 {
				e.AddError("Bounds", "must contain four coordinates")
			}
		case "After":
			if b := govalidator.IsInt(filter.Value); !b {
				e.AddError("After", "must be integer")
			}

			if len(d.Sorters) == 0 {
				e.AddError("After", "must have a sort param")
			}
		case "Before":
			if b := govalidator.IsInt(filter.Value); !b {
				e.AddError("Before", "must be integer")
			}

			if len(d.Sorters) == 0 {
				e.AddError("Before", "must have a sort param")
			}
		}
	}

	if len(e.Errors) == 0 {
		return nil
	} else {
		return e
	}
}
Пример #4
0
func (c Validator) IsModelValid(climb interface{}, ctx validation.Context) error {
	m := climb.(Model)
	e := boom.NewError("Validation error. Please fix the errors before continuing.")

	if b := govalidator.InRange(float64(m.Latitude), -90, 90); !b {
		e.AddError("Latitude", fmt.Sprintf("%v is outside range [-90, 90]", m.Latitude))
	}

	if b := govalidator.InRange(float64(m.Longitude), -180, 180); !b {
		e.AddError("Longitude", fmt.Sprintf("%v is outside range [-180, 180]", m.Longitude))
	}

	if len(e.Errors) == 0 {
		return nil
	} else {
		return e
	}
}
Пример #5
0
func (c Validator) IsCriteriaValid(d decode.Criteria, ctx validation.Context) error {
	e := boom.NewError("Query error. Please fix the errors before continuing.")

	for _, filter := range d.Filters {
		switch filter.Field {
		case "After":
			if b := govalidator.IsInt(filter.Value); !b {
				e.AddError("After", "must be integer")
			}

			if len(d.Sorters) == 0 {
				e.AddError("After", "must have a sort param")
			}
		case "Before":
			if b := govalidator.IsInt(filter.Value); !b {
				e.AddError("Before", "must be integer")
			}

			if len(d.Sorters) == 0 {
				e.AddError("Before", "must have a sort param")
			}
		case "Latitude":
			values := strings.Split(filter.Value, ",")
			for _, value := range values {
				if b := govalidator.IsFloat(value); !b {
					e.AddError("Latitude", "must be numeric")
				}
			}
		case "Longitude":
			values := strings.Split(filter.Value, ",")
			for _, value := range values {
				if b := govalidator.IsFloat(value); !b {
					e.AddError("Longitude", "must be numeric")
				}
			}
		}
	}

	if len(e.Errors) == 0 {
		return nil
	} else {
		return e
	}
}
Пример #6
0
func (c Validator) IsModelValid(climb interface{}, ctx validation.Context) error {
	m := climb.(Model)
	e := boom.NewError("Validation error. Please fix the errors before continuing.")

	if b := govalidator.StringLength(m.Name, "0", "15"); !b {
		e.AddError("Name", "cannot exceed 15 characters")
	}

	if b := govalidator.InRange(float64(m.Rating), 0, 16); !b {
		e.AddError("Rating", fmt.Sprintf("%v is outside range [0, 16]"))
	}

	if b := govalidator.StringLength(m.Description, "0", "255"); !b {
		e.AddError("Description", fmt.Sprintf("cannot exceed 255 characters"))
	}

	if len(e.Errors) == 0 {
		return nil
	} else {
		return e
	}
}
Пример #7
0
func (v CriteriaValidationErrorStub) IsCriteriaValid(decode.Criteria, validation.Context) error {
	e := boom.NewError("Query error. Please fix the errors before continuing.")
	e.AddError("ID", "must be integer")
	return e
}
Пример #8
0
func (v ModelValidationErrorStub) IsModelValid(interface{}, validation.Context) error {
	e := boom.NewError("Validation error. Please fix the errors before continuing.")
	e.AddError("ID", "must be integer")
	return e
}