Ejemplo n.º 1
0
// ServeValidationError returns a Validation Error's list of messages with a validation err code.
func (baseController *BaseController) ServeValidationError() {
	baseController.Ctx.Output.SetStatus(appErrors.VALIDATION_ERROR_CODE)

	msgs := MessageResponse{}
	msgs.Messages = []string{localize.T(appErrors.VALIDATION_ERROR_MSG)}
	baseController.Data["json"] = &msgs
	baseController.ServeJson()
}
Ejemplo n.º 2
0
// Validate validates a type against the valid tags in the type.
func (baseController *BaseController) Validate(params interface{}) bool {
	valid := validation.Validation{}
	ok, err := valid.Valid(params)
	if err != nil {
		baseController.ServeMessageWithStatus(appErrors.VALIDATION_ERROR_CODE, localize.T(appErrors.VALIDATION_ERROR_MSG))
		return false
	}

	if ok == false {
		// Build a map of the error messages for each field
		messages2 := map[string]string{}
		val := reflect.ValueOf(params).Elem()
		for i := 0; i < val.NumField(); i++ {
			// Look for an error tag in the field
			typeField := val.Type().Field(i)
			tag := typeField.Tag
			tagValue := tag.Get("error")

			// Was there an error tag
			if tagValue != "" {
				messages2[typeField.Name] = tagValue
			}
		}

		// Build the error response
		errors := []string{}
		for _, err := range valid.Errors {
			// Match an error from the validation framework errors
			// to a field name we have a mapping for
			message, ok := messages2[err.Field]
			if ok == true {
				// Use a localized message if one exists
				errors = append(errors, localize.T(message))
				continue
			} else {
				// No match, so use the message as is, Formats the err msg to include the key (field name).
				errors = append(errors, fmt.Sprintf("%s %s", err.Field, err.Message))
			}
		}

		baseController.ServeMessagesWithStatus(appErrors.VALIDATION_ERROR_CODE, errors)
		return false
	}

	return true
}
Ejemplo n.º 3
0
// ParseAndValidate is used to parse any form and query parameters from the request and validate the values.
func (baseController *BaseController) ParseAndValidate(obj interface{}) bool {
	err := baseController.ParseForm(obj)
	if err != nil {
		baseController.ServeMessageWithStatus(appErrors.VALIDATION_ERROR_CODE, localize.T(appErrors.VALIDATION_ERROR_MSG))
		return false
	}

	return baseController.Validate(obj)
}
Ejemplo n.º 4
0
// ParseAndValidateJson is used to parse json into a type from the request and validate the values.
func (baseController *BaseController) ParseAndValidateJson(obj interface{}) bool {
	decoder := json.NewDecoder(baseController.Ctx.Request.Body)
	err := decoder.Decode(obj)
	if err != nil {
		baseController.ServeMessageWithStatus(appErrors.VALIDATION_ERROR_CODE, localize.T(appErrors.VALIDATION_ERROR_MSG))
		return false
	}

	return baseController.Validate(obj)
}
Ejemplo n.º 5
0
// ServeError serves a error interface object.
func (baseController *BaseController) ServeError(err error) {
	switch e := err.(type) {
	case *appErrors.AppError:
		if e.ErrorCode() != 0 {
			baseController.ServeMessageWithStatus(e.ErrorCode(), e.Error())
			break
		}

		baseController.ServeMessageWithStatus(appErrors.APP_ERROR_CODE, e.Error())

	default:
		// We want to always return a generic message when an application error exists
		// We don't want to give the end user any information they could use against us
		baseController.ServeMessageWithStatus(appErrors.APP_ERROR_CODE, localize.T(appErrors.APP_ERROR_MSG))
	}
}
Ejemplo n.º 6
0
// ServeUnAuthorized returns an Unauthorized error.
func (baseController *BaseController) ServeUnAuthorized() {
	tracelog.INFO("BaseController", "ServeUnAuthorized", "UnAuthorized, Exiting")

	baseController.ServeMessageWithStatus(appErrors.UNAUTHORIZED_ERROR_CODE, localize.T(appErrors.UNAUTHORIZED_ERROR_MSG))
}
Ejemplo n.º 7
0
// ServeAppError serves a generic application error.
func (baseController *BaseController) ServeAppError() {
	baseController.ServeMessageWithStatus(appErrors.APP_ERROR_CODE, localize.T(appErrors.APP_ERROR_MSG))
}
Ejemplo n.º 8
0
// ServeLocalizedError serves a validation error based on the specified key for the
// translated message.
func (baseController *BaseController) ServeLocalizedError(key string) {
	baseController.ServeMessageWithStatus(appErrors.VALIDATION_ERROR_CODE, localize.T(key))
}