Search code examples
pythondjangocelerypickle

Django Celery getting pickling error on throwing custom exception


I'm using celery in a django application. In case of a failure I raise a custom exception, which is defined with this class:

class CustomException(Exception):
    def __init__(self, err_input, err_info=''):
        self.err_key, self.err_msg, self.app_domain = err_input
        self.message = self.err_msg
        super(CustomException, self).__init__(self.message)
        self.err_info = err_info

    def __str__(self):
        return '[{}] {}'.format(str(self.err_key), str(self.err_msg))

    def __repr__(self):
        return self.message

the error_info is used for an internal logging purpose. If I throw the message I get in the related celery task:

{
  "task_id": "0dfd5ef3-0df1-11eb-b74a-0242ac110002",
  "status": "FAILURE",
  "result": {
    "exc_type": "MaybeEncodingError",
    "exc_message": [
      "'PicklingError(\"Can\\'t pickle <class \\'module.CustomException\\'>: it\\'s not the same object as module.CustomException\")'",
      "\"(1, <ExceptionInfo: CustomException('Error message')>, None)\""
    ],
    "exc_module": "billiard.pool"
  },
  "traceback": null,
  "children": []
}

what am I misconfiguring in my exception class?


Solution

  • The solution was to remove the call to super(CustomException, self).__init__(self.message).

    From what I read Pickling is expecting an init function with only 1 parameter. So to avoid the error I either had to change the init of my CustomException to accept only 1 parameter or remove the call to super