github.com/iDigitalFlame/xmt@v0.5.4/data/error.go (about)

     1  // Copyright (C) 2020 - 2023 iDigitalFlame
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU General Public License as published by
     5  // the Free Software Foundation, either version 3 of the License, or
     6  // any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU General Public License
    14  // along with this program.  If not, see <https://www.gnu.org/licenses/>.
    15  //
    16  
    17  package data
    18  
    19  import (
    20  	"io"
    21  
    22  	"github.com/iDigitalFlame/xmt/util/xerr"
    23  )
    24  
    25  const (
    26  	// ErrTooLarge is raised if memory cannot be allocated to store data in a Chunk.
    27  	ErrTooLarge = dataError(3)
    28  	// ErrInvalidType is an error that occurs when the Bytes, ReadBytes, StringVal
    29  	// or ReadString functions could not properly determine the underlying type
    30  	// of array from the Reader.
    31  	ErrInvalidType = dataError(1)
    32  	// ErrInvalidIndex is raised if a specified Grow or index function is supplied
    33  	// with a negative or out of bounds number or when a Seek index is not valid.
    34  	ErrInvalidIndex = dataError(2)
    35  )
    36  
    37  // ErrLimit is an error that is returned when a Limit is set on a Chunk and the
    38  // size limit was hit when attempting to write to the Chunk. This error wraps the
    39  // io.EOF error, which allows this error to match io.EOF for sanity checking.
    40  var ErrLimit = new(limitError)
    41  
    42  type dataError uint8
    43  type limitError struct{}
    44  
    45  func (limitError) Error() string {
    46  	if xerr.ExtendedInfo {
    47  		return "buffer limit reached"
    48  	}
    49  	return "0x23"
    50  }
    51  func (limitError) Unwrap() error {
    52  	return io.EOF
    53  }
    54  func (e dataError) Error() string {
    55  	if xerr.ExtendedInfo {
    56  		switch e {
    57  		case ErrTooLarge:
    58  			return "buffer is too large"
    59  		case ErrInvalidType:
    60  			return "invalid buffer type"
    61  		case ErrInvalidIndex:
    62  			return "invalid index"
    63  		}
    64  		return "unknown error"
    65  	}
    66  	switch e {
    67  	case ErrTooLarge:
    68  		return "0x26"
    69  	case ErrInvalidType:
    70  		return "0x24"
    71  	case ErrInvalidIndex:
    72  		return "0x25"
    73  	}
    74  	return "0x1"
    75  }