rescript-vscode icon indicating copy to clipboard operation
rescript-vscode copied to clipboard

Code analysis gives warning when catching on exception from other module

Open fhammerschmidt opened this issue 2 years ago • 1 comments

Consider the following example:

module Test = {
  exception Nested

  @raises(Nested)
  let raises = async () => raise(Nested)
}

let raises = async () =>
  try await Test.raises() catch {
  | Test.Nested => Js.log("Not found")
  }

it gives me: raises might raise Nested (test.res:9:12) and is not annotated with @raises(Nested)

However, if I open Test instead:

module Test = {
  exception Nested

  @raises(Nested)
  let raises = async () => raise(Nested)
}

open Test

let raises = async () =>
  try await Test.raises() catch {
  | Nested => Js.log("Not found")
  }

Everything works fine

Issue transferred from (https://github.com/rescript-association/reanalyze/issues/188)

fhammerschmidt avatar Jan 27 '23 12:01 fhammerschmidt

I run into this issue too, when trying to centralize some error messages into a util module.

For a workaround, instead of opening the entire Util module you can alias the exception in an inner Exceptions module, and then open that

exception ResultError(string)

module Exceptions = {
  exception ResultError = ResultError
}

@raises(ResultError)
let getOrRaise = result =>
  switch result {
  | Ok(value) => value
  | Error(err) => raise(ResultError(err))
  }
open Util.Exception
let do_ = async () => (...)

hellos3b avatar Feb 24 '23 22:02 hellos3b