github.com/alimy/mir/v4@v4.1.0/error.go (about)

     1  // Copyright 2022 Michael Li <alimy@gility.net>. All rights reserved.
     2  // Use of this source code is governed by Apache License 2.0 that
     3  // can be found in the LICENSE file.
     4  
     5  package mir
     6  
     7  import (
     8  	"errors"
     9  	"fmt"
    10  )
    11  
    12  // Error indicator error's wraper
    13  type Error interface {
    14  	error
    15  	StatusCode() int
    16  }
    17  
    18  type httpError struct {
    19  	error
    20  	statusCode int
    21  }
    22  
    23  func NewError(statusCode int, err error) Error {
    24  	return &httpError{
    25  		error:      err,
    26  		statusCode: statusCode,
    27  	}
    28  }
    29  
    30  func Errorf(statusCode int, format string, a ...any) Error {
    31  	return &httpError{
    32  		error:      fmt.Errorf(format, a...),
    33  		statusCode: statusCode,
    34  	}
    35  }
    36  
    37  func Errorln(statusCode int, text string) Error {
    38  	return &httpError{
    39  		error:      errors.New(text),
    40  		statusCode: statusCode,
    41  	}
    42  }
    43  
    44  func (e *httpError) StatusCode() int {
    45  	return e.statusCode
    46  }