package errors

// Wrap wraps an error with cause
func Wrap(err *AppError, cause error) *AppError {
	return &AppError{
		Code:       err.Code,
		Message:    err.Message,
		HTTPStatus: err.HTTPStatus,
		Cause:      cause,
	}
}

// WithContext adds context to an AppError
func WithContext(err *AppError, key string, value interface{}) *AppError {
	newErr := &AppError{
		Code:       err.Code,
		Message:    err.Message,
		HTTPStatus: err.HTTPStatus,
		Cause:      err.Cause,
	}
	if err.Context != nil {
		newErr.Context = make(map[string]interface{})
		for k, v := range err.Context {
			newErr.Context[k] = v
		}
	} else {
		newErr.Context = make(map[string]interface{})
	}
	newErr.Context[key] = value
	return newErr
}

// WithMessage creates a new AppError with a custom message
func WithMessage(err *AppError, message string) *AppError {
	return &AppError{
		Code:       err.Code,
		Message:    message,
		HTTPStatus: err.HTTPStatus,
		Cause:      err.Cause,
		Context:    err.Context,
	}
}
