Skip to content

Commit

Permalink
update error handlers in container
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Apr 2, 2021
1 parent 2dbce0f commit a74c271
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
18 changes: 14 additions & 4 deletions container/Container.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package container

import (
"errors"

cconfig "github.com/pip-services3-go/pip-services3-commons-go/config"
"github.com/pip-services3-go/pip-services3-commons-go/errors"
cerr "github.com/pip-services3-go/pip-services3-commons-go/errors"
crefer "github.com/pip-services3-go/pip-services3-commons-go/refer"
cbuild "github.com/pip-services3-go/pip-services3-components-go/build"
"github.com/pip-services3-go/pip-services3-components-go/info"
Expand Down Expand Up @@ -208,14 +210,18 @@ func (c *Container) Open(correlationId string) error {
var err error

if c.references != nil {
return errors.NewInvalidStateError(
return cerr.NewInvalidStateError(
correlationId, "ALREADY_OPENED", "Container was already opened",
)
}

defer func() {
if r := recover(); r != nil {
err, _ = r.(error)
err, ok := r.(error)
if !ok {
msg, _ := r.(string)
err = errors.New(msg)
}
c.logger.Error(correlationId, err, "Failed to start container")
c.Close(correlationId)
}
Expand Down Expand Up @@ -272,7 +278,11 @@ func (c *Container) Close(correlationId string) error {

defer func() {
if r := recover(); r != nil {
err, _ = r.(error)
err, ok := r.(error)
if !ok {
msg, _ := r.(string)
err = errors.New(msg)
}
c.logger.Error(correlationId, err, "Failed to stop container")
}
}()
Expand Down
9 changes: 7 additions & 2 deletions container/ProcessContainer.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"errors"
"fmt"
"os"
"os/signal"
Expand All @@ -25,7 +26,7 @@ Container
Example
container = NewEmptyProcessContainer();
container.Container.AddFactory(NewMyComponentFactory());
container.Run(process.args);
*/
type ProcessContainer struct {
Expand Down Expand Up @@ -152,7 +153,11 @@ func (c *ProcessContainer) printHelp() {

func (c *ProcessContainer) captureErrors(correlationId string) {
if r := recover(); r != nil {
err, _ := r.(error)
err, ok := r.(error)
if !ok {
msg, _ := r.(string)
err = errors.New(msg)
}
c.Logger().Fatal(correlationId, err, "Process is terminated")
os.Exit(1)
}
Expand Down
10 changes: 5 additions & 5 deletions examples/DummyController.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ import (
)

type DummyController struct {
timer *run.FixedRateTimer
logger *log.CompositeLogger
timer *run.FixedRateTimer
logger *log.CompositeLogger
message string
counter int
}

func NewDummyController() *DummyController {
c := &DummyController {
logger: log.NewCompositeLogger(),
c := &DummyController{
logger: log.NewCompositeLogger(),
message: "Hello World!",
counter: 0,
}
Expand Down Expand Up @@ -69,4 +69,4 @@ func (c *DummyController) Close(correlationId string) error {
func (c *DummyController) Notify(correlationId string, args *run.Parameters) {
c.logger.Info(correlationId, "%d - %s", c.counter, c.message)
c.counter++
}
}

0 comments on commit a74c271

Please sign in to comment.