3

I currently have the following:

class MainError:
    def __init__(self, code, message, errorsList):
        self.code = code
        self.message = message
        # List of Error objects
        self.errorsList = errorsList

    def serialize(self):  
        return {           
        'mainErrorCode': self.code, 
        'message': self.message,
        'errors': self.errorsList         
        }


class Error:    
    def __init__(self, field, message):
        self.field = field
        self.message = message

So I would like to return JSON in the format:

{
  "mainErrorCode" : 1024,
  "message" : "Validation Failed",
  "errors" : [
    {
      "field" : "first_name",
      "message" : "First name cannot have fancy characters"
    },
    {
      "field" : "password",
      "message" : "Password cannot be blank"
    }
  ]
}

Currently I am getting the error:

TypeError: <errors.Error instance at 0x329b908> is not JSON serializable

I am using Flask's Jsonify.

return jsonify(errors=mainError.serialize())

I'm guessing that the list is causing the issue. Could someone please help me with the right way of going about this?

PS: There might be some other glaring errors in my approach (I'm quite new to Python =/)

Updated Solution

def serialize(self):  
     return {           
     'mainErrorCode': self.code, 
     'message': self.message,
     'errors': [error.serialize() for error in self.errorsList] 
     }

class Error:    
    def __init__(self, field, message):
        self.field = field
        self.message = message

    def serialize(self): 
        return {           
        'field': self.field, 
        'message': self.message
        }
7
  • I assume it's not the indentation (serialize is module level)? Also, what's the relation (or heirarchy) between Error and MainError and how is it enforced? Commented Mar 7, 2015 at 1:07
  • @ReutSharabani I am not receiving any indentation errors. Is there something wrong with the way I've written it? Commented Mar 7, 2015 at 1:11
  • it's not an error, but serialize should probably part of MainError. I'm not sure though. Commented Mar 7, 2015 at 1:13
  • Ohhh oops.. yeah sorry it is! I didn't indent it correctly when I posted the question. I will fix it. Commented Mar 7, 2015 at 1:14
  • 1
    I have written them in the same module. The MainError object will contain a list (errorsList) of Error objects. Commented Mar 7, 2015 at 1:20

1 Answer 1

3

As the error suggests, you have a list of Error objects that are not serializable. So make them serializable:

class Error:    

    def __init__(self, field, message):
        self.field = field
        self.message = message

    def serialize(self):  
        return {           
            'field': self.field, 
            'message': self.message
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Reut. I have included my new solution based on what you mentioned. Is the for loop the correct way of calling the serialize function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.