code: def write_error(self, status_code, **kwargs):
          
          self.set_header("Content-Type", "application/json")
          if "exc_info" in kwargs:
              etype, exc, traceback = kwargs["exc_info"]
              if hasattr(exc, "messages"):
                  self.write({"errors": exc.messages})
                  if getattr(exc, "headers", None):
                      for name, val in exc.headers.items():
                          self.set_header(name, val)
                  self.finish()
docs: Write errors as JSON.