1
0
mirror of https://github.com/kataras/iris.git synced 2026-01-08 20:41:57 +00:00

Upgrade to new go errors and some minor fixes and improvements including easier debugging of invalid routes and e.t.c.

Former-commit-id: 5809157b952ccc61a67a9861470774b3a6fee024
This commit is contained in:
Gerasimos (Makis) Maropoulos
2019-10-24 18:57:05 +03:00
parent c8236a8d3e
commit 221978e41a
40 changed files with 941 additions and 834 deletions

View File

@@ -1,10 +1,10 @@
package sessions
import (
"errors"
"sync"
"time"
"github.com/kataras/iris/core/errors"
"github.com/kataras/iris/core/memstore"
)

View File

@@ -1,10 +1,9 @@
package sessions
import (
"errors"
"sync"
"time"
"github.com/kataras/iris/core/errors"
)
type (
@@ -78,9 +77,13 @@ func (p *provider) Init(sid string, expires time.Duration) *Session {
return newSession
}
// ErrNotFound can be returned when calling `UpdateExpiration` on a non-existing or invalid session entry.
// It can be matched directly, i.e: `isErrNotFound := sessions.ErrNotFound.Equal(err)`.
var ErrNotFound = errors.New("not found")
// ErrNotFound may be returned from `UpdateExpiration` of a non-existing or
// invalid session entry from memory storage or databases.
// Usage:
// if err != nil && err.Is(err, sessions.ErrNotFound) {
// [handle error...]
// }
var ErrNotFound = errors.New("session not found")
// UpdateExpiration resets the expiration of a session.
// if expires > 0 then it will try to update the expiration and destroy task is delayed.

View File

@@ -1,10 +1,11 @@
package sessions
import (
"reflect"
"strconv"
"sync"
"github.com/kataras/iris/core/errors"
"github.com/kataras/iris/core/memstore"
)
type (
@@ -165,7 +166,54 @@ func (s *Session) GetFlashStringDefault(key string, defaultValue string) string
return defaultValue
}
var errFindParse = errors.New("Unable to find the %s with key: %s. Found? %#v")
// ErrEntryNotFound similar to core/memstore#ErrEntryNotFound but adds
// the value (if found) matched to the requested key-value pair of the session's memory storage.
type ErrEntryNotFound struct {
Err *memstore.ErrEntryNotFound
Value interface{}
}
func (e *ErrEntryNotFound) Error() string {
return e.Err.Error()
}
// Unwrap method implements the dynamic Unwrap interface of the std errors package.
func (e *ErrEntryNotFound) Unwrap() error {
return e.Err
}
// As method implements the dynamic As interface of the std errors package.
// As should be NOT used directly, use `errors.As` instead.
func (e *ErrEntryNotFound) As(target interface{}) bool {
if v, ok := target.(*memstore.ErrEntryNotFound); ok && e.Err != nil {
return e.Err.As(v)
}
v, ok := target.(*ErrEntryNotFound)
if !ok {
return false
}
if v.Value != nil {
if v.Value != e.Value {
return false
}
}
if v.Err != nil {
if e.Err != nil {
return e.Err.As(v.Err)
}
return false
}
return true
}
func newErrEntryNotFound(key string, kind reflect.Kind, value interface{}) *ErrEntryNotFound {
return &ErrEntryNotFound{Err: &memstore.ErrEntryNotFound{Key: key, Kind: kind}, Value: value}
}
// GetInt same as `Get` but returns its int representation,
// if key doesn't exist then it returns -1 and a non-nil error.
@@ -188,7 +236,7 @@ func (s *Session) GetInt(key string) (int, error) {
return strconv.Atoi(vstring)
}
return -1, errFindParse.Format("int", key, v)
return -1, newErrEntryNotFound(key, reflect.Int, v)
}
// GetIntDefault same as `Get` but returns its int representation,
@@ -241,7 +289,7 @@ func (s *Session) GetInt64(key string) (int64, error) {
return strconv.ParseInt(vstring, 10, 64)
}
return -1, errFindParse.Format("int64", key, v)
return -1, newErrEntryNotFound(key, reflect.Int64, v)
}
// GetInt64Default same as `Get` but returns its int64 representation,
@@ -283,7 +331,7 @@ func (s *Session) GetFloat32(key string) (float32, error) {
return float32(vfloat64), nil
}
return -1, errFindParse.Format("float32", key, v)
return -1, newErrEntryNotFound(key, reflect.Float32, v)
}
// GetFloat32Default same as `Get` but returns its float32 representation,
@@ -321,7 +369,7 @@ func (s *Session) GetFloat64(key string) (float64, error) {
return strconv.ParseFloat(vstring, 32)
}
return -1, errFindParse.Format("float64", key, v)
return -1, newErrEntryNotFound(key, reflect.Float64, v)
}
// GetFloat64Default same as `Get` but returns its float64 representation,
@@ -339,7 +387,7 @@ func (s *Session) GetFloat64Default(key string, defaultValue float64) float64 {
func (s *Session) GetBoolean(key string) (bool, error) {
v := s.Get(key)
if v == nil {
return false, errFindParse.Format("bool", key, "nil")
return false, newErrEntryNotFound(key, reflect.Bool, nil)
}
// here we could check for "true", "false" and 0 for false and 1 for true
@@ -352,7 +400,7 @@ func (s *Session) GetBoolean(key string) (bool, error) {
return strconv.ParseBool(vstring)
}
return false, errFindParse.Format("bool", key, v)
return false, newErrEntryNotFound(key, reflect.Bool, v)
}
// GetBooleanDefault same as `Get` but returns its boolean representation,

View File

@@ -2,12 +2,12 @@ package badger
import (
"bytes"
"errors"
"os"
"runtime"
"sync/atomic"
"time"
"github.com/kataras/iris/core/errors"
"github.com/kataras/iris/sessions"
"github.com/dgraph-io/badger"
@@ -47,7 +47,7 @@ var _ sessions.Database = (*Database)(nil)
// It will remove any old session files.
func New(directoryPath string) (*Database, error) {
if directoryPath == "" {
return nil, errors.New("directoryPath is missing")
return nil, errors.New("directoryPath is empty")
}
lindex := directoryPath[len(directoryPath)-1]

View File

@@ -1,12 +1,12 @@
package boltdb
import (
"errors"
"os"
"path/filepath"
"runtime"
"time"
"github.com/kataras/iris/core/errors"
"github.com/kataras/iris/sessions"
bolt "github.com/etcd-io/bbolt"

View File

@@ -1,9 +1,9 @@
package redis
import (
"errors"
"time"
"github.com/kataras/iris/core/errors"
"github.com/kataras/iris/sessions"
"github.com/kataras/golog"
@@ -252,8 +252,13 @@ func closeDB(db *Database) error {
}
var (
// ErrRedisClosed an error with message 'Redis is already closed'
ErrRedisClosed = errors.New("Redis is already closed")
// ErrKeyNotFound an error with message 'Key $thekey doesn't found'
ErrKeyNotFound = errors.New("Key '%s' doesn't found")
// ErrRedisClosed an error with message 'redis: already closed'
ErrRedisClosed = errors.New("redis: already closed")
// ErrKeyNotFound a type of error of non-existing redis keys.
// The producers(the library) of this error will dynamically wrap this error(fmt.Errorf) with the key name.
// Usage:
// if err != nil && errors.Is(err, ErrKeyNotFound) {
// [...]
// }
ErrKeyNotFound = errors.New("key not found")
)

View File

@@ -145,7 +145,7 @@ func (r *RadixDriver) Get(key string) (interface{}, error) {
return nil, err
}
if mn.Nil {
return nil, ErrKeyNotFound.Format(key)
return nil, fmt.Errorf("%s: %w", key, ErrKeyNotFound)
}
return redisVal, nil
}

View File

@@ -83,7 +83,7 @@ func (r *RedigoDriver) Get(key string) (interface{}, error) {
return nil, err
}
if redisVal == nil {
return nil, ErrKeyNotFound.Format(key)
return nil, fmt.Errorf("%s: %w", key, ErrKeyNotFound)
}
return redisVal, nil
}
@@ -256,7 +256,7 @@ func (r *RedigoDriver) GetBytes(key string) ([]byte, error) {
return nil, err
}
if redisVal == nil {
return nil, ErrKeyNotFound.Format(key)
return nil, fmt.Errorf("%s: %w", key, ErrKeyNotFound)
}
return redis.Bytes(redisVal, err)