Raised from request.files during debugging. The idea is that it can provide a better error message than just a generic KeyError/BadRequest.
| 21 | |
| 22 | |
| 23 | class DebugFilesKeyError(KeyError, AssertionError): |
| 24 | """Raised from request.files during debugging. The idea is that it can |
| 25 | provide a better error message than just a generic KeyError/BadRequest. |
| 26 | """ |
| 27 | |
| 28 | def __init__(self, request: Request, key: str) -> None: |
| 29 | form_matches = request.form.getlist(key) |
| 30 | buf = [ |
| 31 | f"You tried to access the file {key!r} in the request.files" |
| 32 | " dictionary but it does not exist. The mimetype for the" |
| 33 | f" request is {request.mimetype!r} instead of" |
| 34 | " 'multipart/form-data' which means that no file contents" |
| 35 | " were transmitted. To fix this error you should provide" |
| 36 | ' enctype="multipart/form-data" in your form.' |
| 37 | ] |
| 38 | if form_matches: |
| 39 | names = ", ".join(repr(x) for x in form_matches) |
| 40 | buf.append( |
| 41 | "\n\nThe browser instead transmitted some file names. " |
| 42 | f"This was submitted: {names}" |
| 43 | ) |
| 44 | self.msg = "".join(buf) |
| 45 | |
| 46 | def __str__(self) -> str: |
| 47 | return self.msg |
| 48 | |
| 49 | |
| 50 | class FormDataRoutingRedirect(AssertionError): |