github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/table/errors.go (about)

     1  // Copyright 2019 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package table
    16  
    17  import (
    18  	"strings"
    19  
    20  	"github.com/dolthub/dolt/go/libraries/doltcore/row"
    21  )
    22  
    23  // BadRow is an error which contains the row and details about what is wrong with it.
    24  type BadRow struct {
    25  	Row     row.Row
    26  	Details []string
    27  }
    28  
    29  // NewBadRow creates a BadRow instance with a given row and error details
    30  func NewBadRow(r row.Row, details ...string) *BadRow {
    31  	return &BadRow{r, details}
    32  }
    33  
    34  // IsBadRow takes an error and returns whether it is a BadRow
    35  func IsBadRow(err error) bool {
    36  	_, ok := err.(*BadRow)
    37  
    38  	return ok
    39  }
    40  
    41  // GetBadRow will retrieve the Row from the BadRow error
    42  func GetBadRowRow(err error) row.Row {
    43  	br, ok := err.(*BadRow)
    44  
    45  	if !ok {
    46  		panic("Call IsBadRow prior to trying to get the BadRowRow")
    47  	}
    48  
    49  	return br.Row
    50  }
    51  
    52  // Error returns a string with error details.
    53  func (br *BadRow) Error() string {
    54  	return strings.Join(br.Details, "\n")
    55  }