I can't catch the proper raise error I am sending(Raising UnicodeDecodeError and getting TypeError)

Viewed 66

I am creating a flask application and whenever I am trying to raise a UnicodeDecodeError from the class Validation and returning to my main try block then instead of going to UnicodeDecodeError exception it's going to Exception block

This is my main block from where I am calling my class Validation and expecting a UnicodeDecodeError

    try:

        validation = Validation(request.files['csvfile'])
        validation.checkextension()
        # columns = validation.check_columns(csv)
        return redirect(request.url)

    except UnicodeDecodeError:
        # return Response("Error : %s"% UnicodeDecodeError)
        return "Please enter a .csv extension"
    except Exception as e:
        print("abc",repr(e))
        # return Response("Error : %s"% Exception)
        return "Exception"

This is my class from where i am raising the error:

class Validation:
def __init__(self,file):
    self.file = file
    self.location = "./uploads/" + str(file.filename)

def checkextension(self):
    try:
        self.file.save(os.path.join("uploads", self.file.filename))
        data = pd.read_csv(self.location)


    except UnicodeDecodeError:
        os.remove("./uploads/" + str(self.file.filename))
        raise UnicodeDecodeError

    except Exception:
        print(Exception)
        raise Exception

Also When I am printing the statement in except Exception as e: I am getting this output:

TypeError('function takes exactly 5 arguments (0 given)')

I am getting TypeError but I am raising UnicodeDecodeError

0 Answers
Related