this repo has no description
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add exceptions to API

garrison b220ea7c 40ac0e0a

+72 -9
+44
lib/errors.ex
··· 1 + defmodule Hobbes.BeginError do 2 + @moduledoc """ 3 + Raised when a transaction fails to get a read version. 4 + """ 5 + defexception [:message, :error] 6 + 7 + @impl true 8 + def exception([error: {:error, err}]) do 9 + %__MODULE__{message: "begin failed with error: #{inspect(err)}", error: err} 10 + end 11 + end 12 + 13 + defmodule Hobbes.CommitError do 14 + @moduledoc """ 15 + Raised when a transaction fails to commit. 16 + """ 17 + defexception [:message, :error, :read_version, :commit_version, :batch_index] 18 + 19 + @impl true 20 + def exception([error: {:error, err}, read_version: read_version, commit_version: commit_version, batch_index: batch_index]) do 21 + %__MODULE__{message: "commit failed with error: #{inspect(err)}", error: err, read_version: read_version, commit_version: commit_version, batch_index: batch_index} 22 + end 23 + end 24 + 25 + defmodule Hobbes.ReadError do 26 + @moduledoc """ 27 + Raised when a read fails. 28 + """ 29 + defexception [:message, :error] 30 + 31 + @impl true 32 + def exception([error: {:error, err}]) do 33 + %__MODULE__{message: "read failed with error: #{inspect(err)}", error: err} 34 + end 35 + end 36 + 37 + defmodule Hobbes.NoTransactionError do 38 + defexception [:message] 39 + 40 + @impl true 41 + def exception([]) do 42 + %__MODULE__{message: "reads and writes can only be performed within a transaction"} 43 + end 44 + end
+8 -6
lib/hobbes.ex
··· 10 10 11 11 defp fetch_txn! do 12 12 case get_txn() do 13 - nil -> raise "This function must be called within a transaction" 13 + nil -> raise(Hobbes.NoTransactionError) 14 14 %TxnState{} = txn -> txn 15 15 end 16 16 end ··· 40 40 defp begin(cluster_name, opts) do 41 41 case Transaction.new(cluster_name, opts) do 42 42 {:ok, %TxnState{} = txn} -> txn 43 - {:error, _err} = error -> raise "Failed to begin transaction: #{inspect(error)}" 43 + {:error, _err} = error -> raise(Hobbes.BeginError, error: error) 44 44 end 45 45 end 46 46 47 47 defp commit(%TxnState{} = txn) do 48 48 case Transaction.commit(txn) do 49 - {:ok, %TxnState{} = txn} -> txn 50 - {:error, _err} = error -> raise "Failed to commit transaction: #{inspect(error)}" 49 + {:ok, %TxnState{} = txn} -> 50 + txn 51 + {:error, {err, %TxnState{} = txn}} -> 52 + raise(Hobbes.CommitError, error: {:error, err}, read_version: txn.read_version, commit_version: txn.commit_version, batch_index: txn.batch_index) 51 53 end 52 54 end 53 55 ··· 59 61 put_txn(txn) 60 62 result 61 63 {:error, _err} = error -> 62 - raise "Failed to read: #{inspect(error)}" 64 + raise(Hobbes.ReadError, error: error) 63 65 end 64 66 end 65 67 ··· 70 72 put_txn(txn) 71 73 result 72 74 {:error, _err} = error -> 73 - raise "Failed to read: #{inspect(error)}" 75 + raise(Hobbes.ReadError, error: error) 74 76 end 75 77 end 76 78
+20 -3
lib/workloads/model.ex
··· 6 6 7 7 alias Trinity.{SimProcess, SimServer} 8 8 9 + import ExUnit.Assertions, only: [assert: 1] 10 + 9 11 @behaviour Hobbes.Workloads.Workload 10 12 11 13 defmodule Client do ··· 67 69 {:clear_range, sk, ek} -> Hobbes.clear_range(sk, ek) 68 70 end) 69 71 72 + Process.put(:_txn_read_results, read_results) 70 73 read_results 71 74 end, return_transaction: true) 72 75 73 76 append_history(state, HistoryTxn.new(:committed, committed_txn, read_results, mutations)) 74 - rescue RuntimeError -> 75 - # TODO: track read_conflict transactions 76 - state 77 + rescue 78 + e in Hobbes.CommitError -> 79 + read_results = Process.get(:_txn_read_results) 80 + assert read_results != nil 81 + Process.delete(:_txn_read_results) 82 + 83 + case e.error do 84 + error when error in [:read_version_too_old, :read_conflict] -> 85 + append_history(state, %HistoryTxn{ 86 + status: error, 87 + read_version: e.read_version, 88 + commit_version: e.commit_version, 89 + batch_index: e.batch_index, 90 + read_results: read_results, 91 + mutations: mutations, 92 + }) 93 + end 77 94 end 78 95 end 79 96