github.com/cloudwego/frugal@v0.1.15/internal/utils/errors.go (about)

     1  /*
     2   * Copyright 2022 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package utils
    18  
    19  import (
    20      `fmt`
    21      `reflect`
    22  )
    23  
    24  type TypeError struct {
    25      Note string
    26      Type reflect.Type
    27  }
    28  
    29  func (self TypeError) Error() string {
    30      return fmt.Sprintf("TypeError(%s): %s", self.Type, self.Note)
    31  }
    32  
    33  type SyntaxError struct {
    34      Pos    int
    35      Src    string
    36      Reason string
    37  }
    38  
    39  func (self SyntaxError) Error() string {
    40      return fmt.Sprintf("Syntax error at position %d: %s", self.Pos, self.Reason)
    41  }
    42  
    43  func EType(vt reflect.Type, note string) TypeError {
    44      return TypeError {
    45          Type: vt,
    46          Note: note,
    47      }
    48  }
    49  
    50  func ESyntax(pos int, src string, reason string) SyntaxError {
    51      return SyntaxError {
    52          Pos    : pos,
    53          Src    : src,
    54          Reason : reason,
    55      }
    56  }
    57  
    58  func ESetList(pos int, src string, vt reflect.Type) SyntaxError {
    59      return ESyntax(pos, src, fmt.Sprintf(`ambiguous type between set<%s> and list<%s>, please specify in the "frugal" tag`, vt, vt))
    60  }
    61  
    62  func EUseOther(vt reflect.Type, alt string) TypeError {
    63      return TypeError {
    64          Type: vt,
    65          Note: fmt.Sprintf("Thrift does not support %s, use %s instead", vt, alt),
    66      }
    67  }