Skip to content

Commit

Permalink
Merge pull request #27 from FHaggs/fix-expression-errors
Browse files Browse the repository at this point in the history
Add expressions for raised errros
  • Loading branch information
Turall authored Nov 20, 2024
2 parents 10d5e42 + 7a1ccd6 commit 73a733c
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions opa_client/opa.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import os
import threading
from typing import Dict, Optional
Expand All @@ -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,
)


Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

Expand All @@ -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:
"""
Expand Down Expand Up @@ -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}"
Expand Down

0 comments on commit 73a733c

Please sign in to comment.