Skip to content

Instantly share code, notes, and snippets.

@jamii
Created February 8, 2012 14:20
Show Gist options
  • Save jamii/1769902 to your computer and use it in GitHub Desktop.
Save jamii/1769902 to your computer and use it in GitHub Desktop.
let recover_with recover f =
try
f ()
with Invalid_transition _ as exc ->
recover exc;
raise exc
let unrecoverable f =
try
f ()
with Invalid_transition _ as exc ->
failwith ("Invalid transition in unrecoverable region: " ^ (Printexc.to_string exc))
let order_create (order_id : uid) (account_id : uid) (market_id : uid) (contract_id : uid) (side : side) (limit_price : price) (quantity : quantity) : unit =
(* create the order before validation because even nonsense orders need to be recorded and rejected *)
let order = Order.create order_id account_id market_id contract_id side limit_price quantity in
Error.recover_with
(fun exc ->
Order.reject order (Printexc.to_string exc))
(fun () ->
let account = Account.find_exn account_id in
Account.validate_order_create account order_id;
Market.validate_order_create market order_id;
Error.unrecoverable
(fun () ->
Account.order_create account order_id;
Market.order_create market order_id;
Order.accept order))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment