How do I interpret printer state string in my plugin?

I'm trying to come up with a robust way to know if printer is in error state.

What I initially did was:

has_error = state['flags']['error']

However, I ran into this:

'state': {'text': "Offline (Error: SerialException: 'device reports readiness to read but returned no data (device disconnec      ted or multiple access on port?)' @ comm.py:_readline:2793)", 'flags': {'cancelling': False, 'paused': False, 'operational': False, 'pausing': False, 'printing': False, 're      suming': False, 'sdReady': False, 'error': False, 'ready': False, 'finishing': False, 'closedOrError': True}}

Then I changed to:

has_error = state['flags']['error'] or  state['flags']['closedOrError']

However, when the printer is disconnected the state string looks like this:

'state': {'text': 'Offline', 'flags': {'cancelling': False, 'paused': False, 'operational': False, 'pausing': False, 'printing': False, 'resuming': False, 'sdReady': False, 'error': False, 'ready': False, 'finishing': False, 'closedOrError': True}}

I have also seen state string like this:

'state': {'text': 'Error: Connection error, see Terminal tab', 'flags': {'cancelling': False, 'paused': False, 'operational': False, 'pausing': False, 'printing': False, 'resuming': False, 'sdReady': False, 'error': True, 'ready': False, 'finishing': False, 'closedOrError': True}},

What will be the robust way to know the printer is in error state and the user should know about it?

It all depends on where you are getting the state you are referring to. I would think using the EventHandlerPlugin mixin and reacting to the printerSateChanged event or using get_stated_id() and evaluating the return. I would tend to go with the printerStateChanged event method.

Thanks @jneilliii for the info.

Sorry that I forgot to mention: I am using _printer.get_current_data() to get a bunch of info about the printer, including the state string that I posted above.

It really depends on what you are trying to report really. Your variable indicates you are only interested in error states since it's named has_error, but then you are also seeing "error" messages that the printer is disconnected from OctoPrint, which is not an error state, but a disconnected state. If you want to monitor both then use closedOrError alone. If you want to know error only use error alone. If you want to know disconnected only use not state['flags']['error'] and state['flags']['closedOrError'].

My initially understanding was the same as yours @jneilliii. However, it doesn't seem to match the reality. My first example was an error and not "closed" (printer was still connected and printing), I saw error=False while closedOrError=True.

@foosel Sorry to tag you again. But maybe you can chime in here?

And I'm only interested in errors, not the disconnected state.