aboutsummaryrefslogtreecommitdiffstats
path: root/plumbing/format/pktline/error.go
blob: 2c0e5a72a9b572e15770b6b09f58a6b5f1e3421a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package pktline

import (
	"bytes"
	"errors"
	"io"
	"strings"
)

var (
	// ErrInvalidErrorLine is returned by Decode when the packet line is not an
	// error line.
	ErrInvalidErrorLine = errors.New("expected an error-line")

	errPrefix = []byte("ERR ")
)

// ErrorLine is a packet line that contains an error message.
// Once this packet is sent by client or server, the data transfer process is
// terminated.
// See https://git-scm.com/docs/pack-protocol#_pkt_line_format
type ErrorLine struct {
	Text string
}

// Error implements the error interface.
func (e *ErrorLine) Error() string {
	return e.Text
}

// Encode encodes the ErrorLine into a packet line.
func (e *ErrorLine) Encode(w io.Writer) error {
	p := NewEncoder(w)
	return p.Encodef("%s%s\n", string(errPrefix), e.Text)
}

// Decode decodes a packet line into an ErrorLine.
func (e *ErrorLine) Decode(r io.Reader) error {
	s := NewScanner(r)
	if !s.Scan() {
		return s.Err()
	}

	line := s.Bytes()
	if !bytes.HasPrefix(line, errPrefix) {
		return ErrInvalidErrorLine
	}

	e.Text = strings.TrimSpace(string(line[4:]))
	return nil
}