error-tools.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. let wrapped_assert = fun (
  2. 'function assert (p: Bool) -> Void',
  3. p => p? Void: panic('Assertion Failed')
  4. )
  5. let wrapped_panic = f (
  6. 'panic',
  7. 'function panic (e: Error) -> Never',
  8. e => {
  9. throw new RuntimeError('panic: ' + e.message)
  10. },
  11. 'function panic (msg: String) -> Never',
  12. msg => {
  13. panic(msg)
  14. }
  15. )
  16. let wrapped_throw = fun (
  17. 'function throw (e: Error) -> Never',
  18. e => {
  19. if (e instanceof CustomError) {
  20. e.trace = get_trace()
  21. throw e
  22. } else {
  23. throw e
  24. }
  25. }
  26. )
  27. function ensure_failed (e, name, args, file, row, col) {
  28. if (e) {
  29. e[DUMP_TYPE] = DUMP_ENSURE
  30. e[DUMP_NAME] = name
  31. e[DUMP_ARGS] = args
  32. }
  33. throw new EnsureFailed(name, file, row, col)
  34. }
  35. function try_failed (e, error, name) {
  36. if (e) {
  37. e[DUMP_TYPE] = DUMP_TRY
  38. e[DUMP_NAME] = name
  39. }
  40. throw error
  41. }
  42. function inject_ensure_args (scope, names, e) {
  43. assert(scope instanceof Scope)
  44. assert(is(names, TypedList.of(Types.String)))
  45. assert(is(e[DUMP_ARGS], Types.List))
  46. foreach(names, (name, i) => {
  47. if (i < e[DUMP_ARGS].length) {
  48. scope.declare(name, e[DUMP_ARGS][i])
  49. } else {
  50. scope.declare(name, Nil)
  51. }
  52. })
  53. }