lorenzodonini / ocpp-go

Open Charge Point Protocol implementation in Go
MIT License
277 stars 126 forks source link

Handle incoming invalid messages #215

Closed lorenzodonini closed 1 year ago

lorenzodonini commented 1 year ago

Adds a hook for client and servers to support custom handling of incoming invalid messages.

This refers to invalid payloads only:

The hooks are completely optional and may be set as follows:

// Client
ocppjClient.SetInvalidMessageHook(func(err *ocpp.Error, rawMessage string, parsedFields []interface{}) *ocpp.Error {
     // custom handling
     return ocpp.NewError(...) // if no error override is necessary, nil can be returned
})

// Server
ocppjServer.SetInvalidMessageHook(func(client ws.Channel, err *ocpp.Error, rawMessage string, parsedFields []interface{}) *ocpp.Error {
     // custom handling
     return ocpp.NewError(...) // if no error override is necessary, nil can be returned
}

If set, a hook MUST return a value as soon as possible, as the function call blocks the main routine from receiving any other messages.

If the returned value is nil the internal error will be sent back to the other endpoint. If the returned value is actually a valid error, it will be sent back to the other endpoint instead of the internally generated error.

Addresses the issue described here.