github.com/tursom/GoCollections@v0.3.10/lang/PackageException.go (about) 1 /* 2 * Copyright (c) 2022 tursom. All rights reserved. 3 * Use of this source code is governed by a GPL-3 4 * license that can be found in the LICENSE file. 5 */ 6 7 package lang 8 9 import ( 10 "fmt" 11 "reflect" 12 ) 13 14 type PackageException struct { 15 RuntimeException 16 err any 17 } 18 19 func NewPackageException(err any, config *ExceptionConfig) *PackageException { 20 message := "" 21 switch e := err.(type) { 22 case error: 23 message = e.Error() 24 default: 25 message = fmt.Sprint(e) 26 } 27 t := reflect.TypeOf(err) 28 message = fmt.Sprintf("%s (%s)", message, t.Name()) 29 return &PackageException{ 30 RuntimeException: *NewRuntimeException(message, config.AddSkipStack(1). 31 SetExceptionName("github.com.tursom.GoCollections.exceptions.PackageException")), 32 err: err, 33 } 34 } 35 36 func (p *PackageException) Err() any { 37 return p.err 38 } 39 40 func UnpackException(err any) any { 41 for err != nil { 42 switch e := err.(type) { 43 case *PackageException: 44 return e.Err() 45 case Exception: 46 err = e.Cause() 47 if err == nil { 48 return e 49 } 50 default: 51 return err 52 } 53 } 54 return nil 55 }