Repository: koaning/clumper

Issue:
Let's remove "Error occured during writing JSONL file"
This is the result of a failing pytest on my side. 


```
    def write_jsonl(self, path, sort_keys=False, indent=None):
        """
        Writes to a jsonl file.
    
        Arguments:
            path: filename
            sort_keys: If sort_keys is true (default: False), then the output of dictionaries will be sorted by key.
            indent: If indent is a non-negative integer (default: None), then JSON array elements members will be pretty-printed with that indent level.
        Usage:
    
        ```python
        from clumper import Clumper
        clump_orig = Clumper.read_jsonl("tests/data/cards.jsonl")
        clump_orig.write_jsonl("tests/data/cards_copy.jsonl")
    
        clump_copy = Clumper.read_jsonl("tests/data/cards_copy.jsonl")
    
        assert clump_copy.collect() == clump_orig.collect()
        ```
        """
    
        try:
            # Create a new file and open it for writing
            with open(path, "x") as f:
                for current_line_nr, json_dict in enumerate(self.collect()):
                    f.write(
                        json.dumps(json_dict, sort_keys=sort_keys, indent=indent) + "\n"
                    )
    
        except Exception:
>           raise RuntimeError("Error occured during writing JSONL file")
E           RuntimeError: Error occured during writing JSONL file

clumper/clump.py:276: RuntimeError
```

The message `Error occured during writing JSONL file` is making it harder for me to understand what is actually going on. Can we just maybe remove it? 

The error here was that I was trying to write a file that already exists. Instead of giving me this error I got the uninformative "Error occured during writing JSONL file" message.

Discussion:
koaning: Did you remove all of them? I think we also want to remove the ones in the `write_*` functions. 
koaning: Once this change is in I'll make a new release :) 
samarpan-rai: > Did you remove all of them? I think we also want to remove the ones in the `write_*` functions.

Issue #52 only specified jsonl so I didn't remove the ones in json. It should be done now. I should really learn to not over-engineer stuff 😅

Patch:
diff --git a/clumper/clump.py b/clumper/clump.py
index b95ffb4..6be9a4d 100644
--- a/clumper/clump.py
+++ b/clumper/clump.py
@@ -116,28 +116,24 @@ class Clumper:
             if n <= 0:
                 raise ValueError("Number of lines to read must be > 0.")
 
-        try:
-            # Case 1 : Open cloud file in stream
-            if path.startswith("https:") or path.startswith("http:"):
-                f = urllib.request.urlopen(path)
-            # Case 2 : Local file
-            else:
-                f = open(path)
-
-            # Initalize a place to store the parsed data as list
-            data_array = []
-            # Read it, parse and close it
-            with f:
-                for current_line_nr, json_string in enumerate(f):
-                    if n is not None and current_line_nr == n:
-                        break
-                    json_object = json.loads(json_string)
-                    data_array.append(json_object)
-            # Return it
-            return Clumper(data_array)
-
-        except Exception:
-            raise RuntimeError("Error occured during reading in JSONL file")
+        # Case 1 : Open cloud file in stream
+        if path.startswith("https:") or path.startswith("http:"):
+            f = urllib.request.urlopen(path)
+        # Case 2 : Local file
+        else:
+            f = open(path)
+
+        # Initialize a place to store the parsed data as list
+        data_array = []
+        # Read it, parse and close it
+        with f:
+            for current_line_nr, json_string in enumerate(f):
+                if n is not None and current_line_nr == n:
+                    break
+                json_object = json.loads(json_string)
+                data_array.append(json_object)
+        # Return it
+        return Clumper(data_array)
 
     @classmethod
     def read_yaml(cls, path: str, n=None):
@@ -242,13 +238,9 @@ class Clumper:
         assert clump_copy.collect() == clump_orig.collect()
         ```
         """
-
-        try:
-            # Create a new file and open it for writing
-            with open(path, "w") as f:
-                json.dump(self.collect(), f, sort_keys=sort_keys, indent=indent)
-        except Exception:
-            raise RuntimeError("Error occured during writing JSON file")
+        # Create a new file and open it for writing
+        with open(path, "w") as f:
+            json.dump(self.collect(), f, sort_keys=sort_keys, indent=indent)
 
     def write_jsonl(self, path, sort_keys=False, indent=None):
         """


Test patch:
diff --git a/tests/test_read_write/test_read_jsonl.py b/tests/test_read_write/test_read_jsonl.py
index a812373..351b6ce 100644
--- a/tests/test_read_write/test_read_jsonl.py
+++ b/tests/test_read_write/test_read_jsonl.py
@@ -29,5 +29,5 @@ def test_read_csv_negative_zero():
 
 
 def test_non_existing_file():
-    with pytest.raises(RuntimeError):
+    with pytest.raises(FileNotFoundError):
         Clumper.read_jsonl("tests/cards.jsonl")