From aed882000ac9e720bdc398013c82c6c001c38a19 Mon Sep 17 00:00:00 2001 From: Danny Xu <98006139+d-bytebase@users.noreply.github.com> Date: Sat, 21 Jan 2023 12:54:43 +0800 Subject: [PATCH] chore: use Read() for reading error buffer (#4366) (#4377) --- plugin/db/pg/dump.go | 33 ++++++++------------------------- 1 file changed, 8 insertions(+), 25 deletions(-) diff --git a/plugin/db/pg/dump.go b/plugin/db/pg/dump.go index 88b1aa2956a56e..9a049e8f59aa6e 100644 --- a/plugin/db/pg/dump.go +++ b/plugin/db/pg/dump.go @@ -146,34 +146,17 @@ func (driver *Driver) dumpOneDatabaseWithPgDump(ctx context.Context, database st } } - var errBuilder strings.Builder - for { - line, readErr := errReader.ReadString('\n') - if readErr != nil && readErr != io.EOF { - return readErr - } - - if err := func() error { - // Log the error, but return the first 1024 characters in the error to users. - log.Warn(line) - if errBuilder.Len() < 1024 { - if _, err := errBuilder.WriteString(line); err != nil { - return err - } - } - return nil - }(); err != nil { - return err - } - - if readErr == io.EOF { - break - } + errorMsg := make([]byte, 1024) + readSize, readErr := errReader.Read(errorMsg) + if readErr != nil && readErr != io.EOF { + return err + } + if readSize > 0 { + log.Warn(string(errorMsg)) } - err = cmd.Wait() if err != nil { - return errors.Wrapf(err, "error message: %s", errBuilder.String()) + return errors.Wrapf(err, "error message: %s", errorMsg) } return nil }