"""Log parsing functions for Micro."""
from __future__ import absolute_import
from json import load
from pkg_resources import resource_filename


def init(state):
    """Init Micro logs."""
    filename = resource_filename(
        'logparser.logs.micro', 'error_logs.json')

    with open(filename) as json_errors:
        state["json_errors"] = load(json_errors)


def on_micro_error(match, state, logger):
    """Error on Micro was thrown."""
    module_id = match[2]
    error_id = match[3]
    errors = state["json_errors"]

    error_description = errors[module_id][error_id]["description"]
    error_name = errors[module_id][error_id]["name"]

    logger.error("[" + error_name + "] " + error_description)

"""Log parsing functions for Micro."""
from __future__ import absolute_import
from json import load
from pkg_resources import resource_filename


def init(state):
    """Init Micro logs."""
    filename = resource_filename(
        'logparser.logs.micro', 'error_logs.json')

    with open(filename) as json_errors:
        state["json_errors"] = load(json_errors)


def on_micro_error(match, state, logger):
    """Error on Micro was thrown."""
    module_id = match[2]
    error_id = match[3]
    errors = state["json_errors"]

    if module_id in errors:
        module = errors[module_id]
        if error_id in module:
            error_description = module[error_id]["description"]
            error_name = module[error_id]["name"]
            logger.error("[" + error_name + "] " + error_description)

Check if module and error code exist
Check if module and error code exist

  """Log parsing functions for Micro."""
  from __future__ import absolute_import
  from json import load
  from pkg_resources import resource_filename
  
  
  def init(state):
      """Init Micro logs."""
      filename = resource_filename(
          'logparser.logs.micro', 'error_logs.json')
  
      with open(filename) as json_errors:
          state["json_errors"] = load(json_errors)
  
  
  def on_micro_error(match, state, logger):
      """Error on Micro was thrown."""
      module_id = match[2]
      error_id = match[3]
      errors = state["json_errors"]
  
+     if module_id in errors:
+         module = errors[module_id]
+         if error_id in module:
-     error_description = errors[module_id][error_id]["description"]
+             error_description = module[error_id]["description"]
-     error_name = errors[module_id][error_id]["name"]
+             error_name = module[error_id]["name"]
+             logger.error("[" + error_name + "] " + error_description)
  
-     logger.error("[" + error_name + "] " + error_description)
- 
Check if module and error code exist
## Code Before:
"""Log parsing functions for Micro."""
from __future__ import absolute_import
from json import load
from pkg_resources import resource_filename


def init(state):
    """Init Micro logs."""
    filename = resource_filename(
        'logparser.logs.micro', 'error_logs.json')

    with open(filename) as json_errors:
        state["json_errors"] = load(json_errors)


def on_micro_error(match, state, logger):
    """Error on Micro was thrown."""
    module_id = match[2]
    error_id = match[3]
    errors = state["json_errors"]

    error_description = errors[module_id][error_id]["description"]
    error_name = errors[module_id][error_id]["name"]

    logger.error("[" + error_name + "] " + error_description)

## Instruction:
Check if module and error code exist
## Code After:
"""Log parsing functions for Micro."""
from __future__ import absolute_import
from json import load
from pkg_resources import resource_filename


def init(state):
    """Init Micro logs."""
    filename = resource_filename(
        'logparser.logs.micro', 'error_logs.json')

    with open(filename) as json_errors:
        state["json_errors"] = load(json_errors)


def on_micro_error(match, state, logger):
    """Error on Micro was thrown."""
    module_id = match[2]
    error_id = match[3]
    errors = state["json_errors"]

    if module_id in errors:
        module = errors[module_id]
        if error_id in module:
            error_description = module[error_id]["description"]
            error_name = module[error_id]["name"]
            logger.error("[" + error_name + "] " + error_description)
