Skip to content

Commit

Permalink
added logger to log exceptions in routes
Browse files Browse the repository at this point in the history
  • Loading branch information
niklastheman committed Jan 29, 2025
1 parent 8246ac3 commit c762710
Show file tree
Hide file tree
Showing 10 changed files with 145 additions and 68 deletions.
25 changes: 17 additions & 8 deletions fedn/network/api/v1/client_routes.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from flask import Blueprint, jsonify, request

from fedn.common.config import get_controller_config, get_network_config
from fedn.common.log_config import logger
from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.shared import client_store, control, get_checksum, package_store
from fedn.network.api.v1.shared import api_version, get_post_data_to_kwargs, get_typed_list_headers
Expand Down Expand Up @@ -116,7 +117,8 @@ def get_clients():
response = client_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -197,7 +199,8 @@ def list_clients():
response = client_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -258,7 +261,8 @@ def get_clients_count():
count = client_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -311,7 +315,8 @@ def clients_count():
count = client_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -355,7 +360,8 @@ def get_client(id: str):
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -399,7 +405,8 @@ def delete_client(id: str):
return jsonify({"message": msg}), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -503,7 +510,8 @@ def add_client():
"helper_type": helper_type,
}
return jsonify(payload), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"success": False, "message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -555,5 +563,6 @@ def get_client_config():
payload["checksum"] = checksum_str

return jsonify(payload), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500
22 changes: 15 additions & 7 deletions fedn/network/api/v1/combiner_routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint, jsonify, request

from fedn.common.log_config import logger
from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.shared import client_store, combiner_store
from fedn.network.api.v1.shared import api_version, get_post_data_to_kwargs, get_typed_list_headers
Expand Down Expand Up @@ -107,7 +108,8 @@ def get_combiners():
response = combiner_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -186,7 +188,8 @@ def list_combiners():
response = combiner_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -233,7 +236,8 @@ def get_combiners_count():
count = combiner_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -282,7 +286,8 @@ def combiners_count():
count = combiner_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -326,7 +331,8 @@ def get_combiner(id: str):
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -369,7 +375,8 @@ def delete_combiner(id: str):
return jsonify({"message": msg}), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -414,5 +421,6 @@ def number_of_clients_connected():
result = {"result": response}

return jsonify(result), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500
7 changes: 5 additions & 2 deletions fedn/network/api/v1/helper_routes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from flask import Blueprint, jsonify, request

from fedn.common.log_config import logger
from fedn.network.api.auth import jwt_auth_required
from fedn.network.api.shared import package_store
from fedn.network.api.v1.shared import api_version
Expand Down Expand Up @@ -32,7 +33,8 @@ def get_active_helper():
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": "No active helper"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand All @@ -58,5 +60,6 @@ def set_active_helper():
return jsonify({"message": "Active helper set"}), 200
except ValueError:
return jsonify({"message": "Helper is required to be either 'numpyhelper', 'binaryhelper' or 'androidhelper'"}), 400
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500
42 changes: 28 additions & 14 deletions fedn/network/api/v1/model_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def get_models():
response = model_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -187,7 +188,8 @@ def list_models():
response = model_store.list(limit, skip, sort_key, sort_order, **kwargs)

return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -235,7 +237,8 @@ def get_models_count():
count = model_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -287,7 +290,8 @@ def models_count():
count = model_store.count(**kwargs)
response = count
return jsonify(response), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -333,7 +337,8 @@ def get_model(id: str):
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -398,7 +403,8 @@ def patch_model(id: str):
return jsonify({"message": f"Failed to update model: {message}"}), 500
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -455,7 +461,8 @@ def put_model(id: str):
return jsonify({"message": f"Failed to update model: {message}"}), 500
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -509,7 +516,8 @@ def get_descendants(id: str):
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -578,7 +586,8 @@ def get_ancestors(id: str):
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -627,7 +636,8 @@ def download(id: str):
return jsonify({"message": "No model storage configured"}), 500
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -692,7 +702,8 @@ def get_parameters(id: str):
return jsonify({"message": "No model storage configured"}), 500
except EntityNotFound:
return jsonify({"message": f"Entity with id: {id} not found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -725,7 +736,8 @@ def get_active_model():
return jsonify(response), 200
except EntityNotFound:
return jsonify({"message": "No active model found"}), 404
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -766,7 +778,8 @@ def set_active_model():
return jsonify({"message": "Active model set"}), 200
else:
return jsonify({"message": "Failed to set active model"}), 500
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500


Expand Down Expand Up @@ -832,5 +845,6 @@ def upload_model():
"message": "Model added successfully",
}
), 200
except Exception:
except Exception as e:
logger.error(f"An unexpected error occurred: {e}")
return jsonify({"message": "An unexpected error occurred"}), 500
Loading

0 comments on commit c762710

Please sign in to comment.