mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-29 13:49:28 +08:00

* Add : errgroup to cmd/auth Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : Handle graceful stop for auth service Remove : errgroups from auth service Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : Wait till server shutdown Signed-off-by: Arvindh <arvindh91@gmail.com> * Change : instead of waitgroup changed to errgroups Signed-off-by: Arvindh <arvindh91@gmail.com> * change : KillSignalHandler return type to error Signed-off-by: Arvindh <arvindh91@gmail.com> * Empty Commit Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : Context to http server shutdown Rename : varaible from proto to protocol Signed-off-by: Arvindh <arvindh91@gmail.com> * change : to default log level Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : Sign-off Signed-off-by: Arvindh <arvindh91@gmail.com> * Add: graceful stop of http and grpc server Signed-off-by: Arvindh <arvindh91@gmail.com> * Fix: typos and caps Signed-off-by: Arvindh <arvindh91@gmail.com> * Add: Signed-off Signed-off-by: Arvindh <arvindh91@gmail.com> * Rename: Func KillSignalHandler to SignalHandler Add: SIGABRT Signed-off-by: Arvindh <arvindh91@gmail.com> * Fix: auth service Signed-off-by: Arvindh <arvindh91@gmail.com> * Add: timeout for grpc gracefulstop Fix: typos Signed-off-by: Arvindh <arvindh91@gmail.com> * Add: .vscode folder to git ignore Signed-off-by: Arvindh <arvindh91@gmail.com> * change: variable name to stopWaitTime Signed-off-by: Arvindh <arvindh91@gmail.com> * remove: .vscode folder Signed-off-by: Arvindh <arvindh91@gmail.com> * remove: .vscode from .gitignore Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : logger to handlers Signed-off-by: Arvindh <arvindh91@gmail.com> * Add : New line at end of .gitignore file Signed-off-by: Arvindh <arvindh91@gmail.com> * Fix : variable naming Add : graceful stop for timescale Signed-off-by: Arvindh <arvindh91@gmail.com> * Remove : unsued NATS library from import Signed-off-by: Arvindh <arvindh91@gmail.com> * Move: "https" and "https" to moved to const var Signed-off-by: Arvindh <arvindh91@gmail.com> * Move: "http" and "https" to moved to const var Signed-off-by: Arvindh <arvindh91@gmail.com> * update: branch with master Signed-off-by: Arvindh <arvindh91@gmail.com> Co-authored-by: Dušan Borovčanin <dusan.borovcanin@mainflux.com> Co-authored-by: Drasko DRASKOVIC <drasko.draskovic@gmail.com>
116 lines
1.9 KiB
Go
116 lines
1.9 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package errors
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"syscall"
|
|
)
|
|
|
|
// Error specifies an API that must be fullfiled by error type
|
|
type Error interface {
|
|
|
|
// Error implements the error interface.
|
|
Error() string
|
|
|
|
// Msg returns error message
|
|
Msg() string
|
|
|
|
// Err returns wrapped error
|
|
Err() Error
|
|
}
|
|
|
|
var _ Error = (*customError)(nil)
|
|
|
|
// customError represents a Mainflux error
|
|
type customError struct {
|
|
msg string
|
|
err Error
|
|
}
|
|
|
|
func (ce *customError) Error() string {
|
|
if ce == nil {
|
|
return ""
|
|
}
|
|
if ce.err == nil {
|
|
return ce.msg
|
|
}
|
|
return ce.msg + " : " + ce.err.Error()
|
|
}
|
|
|
|
func (ce *customError) Msg() string {
|
|
return ce.msg
|
|
}
|
|
|
|
func (ce *customError) Err() Error {
|
|
return ce.err
|
|
}
|
|
|
|
// Contains inspects if e2 error is contained in any layer of e1 error
|
|
func Contains(e1 error, e2 error) bool {
|
|
if e1 == nil || e2 == nil {
|
|
return e2 == e1
|
|
}
|
|
ce, ok := e1.(Error)
|
|
if ok {
|
|
if ce.Msg() == e2.Error() {
|
|
return true
|
|
}
|
|
return Contains(ce.Err(), e2)
|
|
}
|
|
return e1.Error() == e2.Error()
|
|
}
|
|
|
|
// Wrap returns an Error that wrap err with wrapper
|
|
func Wrap(wrapper error, err error) error {
|
|
if wrapper == nil || err == nil {
|
|
return wrapper
|
|
}
|
|
if w, ok := wrapper.(Error); ok {
|
|
return &customError{
|
|
msg: w.Msg(),
|
|
err: cast(err),
|
|
}
|
|
}
|
|
return &customError{
|
|
msg: wrapper.Error(),
|
|
err: cast(err),
|
|
}
|
|
}
|
|
|
|
func cast(err error) Error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
if e, ok := err.(Error); ok {
|
|
return e
|
|
}
|
|
return &customError{
|
|
msg: err.Error(),
|
|
err: nil,
|
|
}
|
|
}
|
|
|
|
// New returns an Error that formats as the given text.
|
|
func New(text string) Error {
|
|
return &customError{
|
|
msg: text,
|
|
err: nil,
|
|
}
|
|
}
|
|
|
|
func SignalHandler(ctx context.Context) error {
|
|
c := make(chan os.Signal)
|
|
signal.Notify(c, syscall.SIGINT, syscall.SIGABRT)
|
|
select {
|
|
case sig := <-c:
|
|
return fmt.Errorf("%s", sig)
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
}
|