From 7a1ccd6b0f5ab5365827908eb092e01ef86b9bfc Mon Sep 17 00:00:00 2001 From: Pedro Fam Haggstram Date: Tue, 12 Nov 2024 18:14:50 -0300 Subject: [PATCH] Add expressions for raised errros --- opa_client/opa.py | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/opa_client/opa.py b/opa_client/opa.py index de629c3..8d01d10 100644 --- a/opa_client/opa.py +++ b/opa_client/opa.py @@ -1,4 +1,3 @@ -import json import os import threading from typing import Dict, Optional @@ -10,15 +9,15 @@ from .base import BaseClient from .errors import ( - CheckPermissionError, - ConnectionsError, - DeleteDataError, - DeletePolicyError, - FileError, - PathNotFoundError, - PolicyNotFoundError, - RegoParseError, - TypeException, + CheckPermissionError, + ConnectionsError, + DeleteDataError, + DeletePolicyError, + FileError, + PathNotFoundError, + PolicyNotFoundError, + RegoParseError, + TypeException, ) @@ -202,7 +201,7 @@ def update_policy_from_file(self, filepath: str, endpoint: str) -> bool: bool: True if the policy was successfully updated. """ if not os.path.isfile(filepath): - raise FileError(f"'{filepath}' is not a valid file") + raise FileError("file_not_found",f"'{filepath}' is not a valid file") with open(filepath, "r", encoding="utf-8") as file: policy_str = file.read() @@ -300,7 +299,7 @@ def policy_to_file( policy_raw = policy.get("result", {}).get("raw", "") if not policy_raw: - raise PolicyNotFoundError("Policy content is empty") + raise PolicyNotFoundError("resource_not_found", "Policy content is empty") full_path = os.path.join(path or "", filename) @@ -309,7 +308,7 @@ def policy_to_file( file.write(policy_raw) return True except OSError as e: - raise PathNotFoundError(f"Failed to write to '{full_path}'") from e + raise PathNotFoundError("path_not_found", f"Failed to write to '{full_path}'") from e def get_policy(self, policy_name: str) -> dict: """ @@ -395,7 +394,7 @@ def check_permission( if rule_name not in rules: raise CheckPermissionError( - f"Rule '{rule_name}' not found in policy '{policy_name}'" + "resource_not_found", f"Rule '{rule_name}' not found in policy '{policy_name}'" ) url = f"{self.root_url}/{package_path}/{rule_name}"