예제 #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
}
예제 #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
}
예제 #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
}
예제 #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
}
예제 #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)
}
예제 #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
}
예제 #7
0
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))
}
예제 #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
}
예제 #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
}
예제 #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
}
예제 #11
0
파일: wrap.go 프로젝트: jgroeneveld/gotrix
// 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)
}
예제 #12
0
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))
}
예제 #13
0
func Validation(fieldErrors map[string][]string) error {
	return errors.Wrap(&Error{
		Err: &validationError{FieldErrors: fieldErrors},
	})
}
예제 #14
0
func RecordNotFound() error {
	return errors.Wrap(&Error{
		Err: recordNotFoundErr,
	})
}