Beispiel #1
0
func (m *SimpleTxManager) Close(success bool) error {
	if m.didBeginTx {
		if success {
			return errors.Wrap(m.tx.Commit())
		}
		return errors.Wrap(m.tx.Rollback())
	}

	m.tx = nil
	m.didBeginTx = false
	return nil
}
Beispiel #2
0
func (s *TestClient) Delete(path string) (status int, responseBody io.ReadCloser, err error) {
	req, err := http.NewRequest("DELETE", s.testServer.URL+path, nil)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	return rsp.StatusCode, rsp.Body, nil
}
Beispiel #3
0
func (s *TestClient) PatchJSON(path, jsonBody string) (status int, responseBody io.ReadCloser, err error) {
	body := bytes.NewBufferString(jsonBody)

	req, err := http.NewRequest("PATCH", s.testServer.URL+path, body)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}
	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	return rsp.StatusCode, rsp.Body, nil
}
Beispiel #4
0
func (s *TestClient) PostFormData(path string, contentType string, data io.Reader) (status int, responseBody io.ReadCloser, err error) {
	req, err := http.NewRequest("POST", s.testServer.URL+path, data)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}
	req.Header.Add("Content-Type", contentType)

	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	return rsp.StatusCode, rsp.Body, nil
}
Beispiel #5
0
func WithTx(con *sql.DB, f func(Tx) error) error {
	tx, err := con.Begin()
	if err != nil {
		return errors.Wrap(err)
	}

	err = f(tx)
	if err != nil {
		_ = tx.Rollback()
		return err
	}
	cerr := tx.Commit()
	return errors.Wrap(cerr)
}
Beispiel #6
0
func (s *TestClient) PostForm(path string, data string) (status int, responseBody io.ReadCloser, err error) {
	body := bytes.NewBufferString(data)

	req, err := http.NewRequest("POST", s.testServer.URL+path, body)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}
	req.Header.Add("Content-Type", "application/x-www-form-urlencoded")

	rsp, err := http.DefaultClient.Do(req)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}
	return rsp.StatusCode, rsp.Body, nil
}
func Expense(w io.Writer, expense *model.Expense) error {
	rep := &expenseRep{
		Amount:      expense.Amount,
		Description: expense.Description,
	}

	return errors.Wrap(json.NewEncoder(w).Encode(rep))
}
Beispiel #8
0
func (s *TestClient) Get(path string) (status int, responseBody io.ReadCloser, err error) {
	rsp, err := http.Get(s.testServer.URL + path)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	return rsp.StatusCode, rsp.Body, nil
}
Beispiel #9
0
func (s *TestClient) PostJSON(path, jsonBody string) (status int, responseBody io.ReadCloser, err error) {
	body := bytes.NewBufferString(jsonBody)
	rsp, err := http.Post(s.testServer.URL+path, "application/json", body)
	if err != nil {
		return 0, nil, errors.Wrap(err)
	}

	return rsp.StatusCode, rsp.Body, nil
}
Beispiel #10
0
func (m *SimpleTxManager) Begin() (Con, error) {
	tx, err := m.txFactory.BeginTx()
	if err != nil {
		return nil, errors.Wrap(err)
	}

	m.tx = tx
	m.didBeginTx = true
	return tx, nil
}
Beispiel #11
0
// wrap wraps and converts database errors into application level errors
func wrap(err error) error {
	if err == nil {
		return nil
	}

	if err == sql.ErrNoRows {
		return apperrors.RecordNotFound()
	}

	return errors.Wrap(err)
}
func ExpensesList(w io.Writer, expenses []*model.Expense) error {
	rep := []*expenseRep{}

	for _, expense := range expenses {
		rep = append(rep, &expenseRep{
			Amount:      expense.Amount,
			Description: expense.Description,
		})
	}

	return errors.Wrap(json.NewEncoder(w).Encode(rep))
}
Beispiel #13
0
func Validation(fieldErrors map[string][]string) error {
	return errors.Wrap(&Error{
		Err: &validationError{FieldErrors: fieldErrors},
	})
}
Beispiel #14
0
func RecordNotFound() error {
	return errors.Wrap(&Error{
		Err: recordNotFoundErr,
	})
}