Should I reuse the same error number in different stored procedures when throwing errors?

Rob*_*131 0 sql-server f# throw

I'm using Giraffe framework to implement a web server in F#. The web server executes some stored procedures based on client requests. Errors are thrown inside stored procedures when the request cannot be processed and I'm catching the errors in Giraffe like this:

try
     // parse request ...
    // data access and execute stored procdeure ...
with
| :? System.Data.SqlClient.SqlException as e ->
     match e.Number with
    | 60000 ->
        return! (setStatusCode 400 >=> json e.Message) next ctx
Run Code Online (Sandbox Code Playgroud)

Let's say in stored procedure A, I'm throwing an error with error number 60000, error message Error in stored procedure A. In another stored procedure B, can I reuse the same error number with a different error message or should I use a different one?

If I don't reuse the same error number the match expression will continue to grow like the following:

  with
  | :? System.Data.SqlClient.SqlException as e ->
            match e.Number with
            | 60000 ->
                return! (setStatusCode 400 >=> json e.Message) next ctx
            | 60001 ->
                return! (setStatusCode 400 >=> json e.Message) next ctx
            // more error numbers in the future ...
Run Code Online (Sandbox Code Playgroud)

tor*_*nde 5

I've never actually used error codes in SQL, so I don't know what is best practice, but I would wager it is using one code for one type of error and one message. What you're suggesting is bending that suggestion, for the sole purpose of making an entirely other part of your code shorter.

Even if you do create a lot of error codes, you can avoid duplication in your code by doing

try ...
with
| :? SqlException as e ->
    match e.Number with
    | 60000 | 60001 ->
        return! (setStatusCode 400 >=> json e.Message) next ctx
Run Code Online (Sandbox Code Playgroud)

If this is something you need to do in several places, consider pulling this out into its own, separate function. If the list of error codes becomes large, consider putting this in a list object instead, e.g.

open System.Linq

let sqlErrorCodes = [ 60000; 60001 ]

try ...
with :? SqlException e when sqlErrorCodes.Contains(e.Number) ->
    return! (setStatusCode 400 >=> json e.Message) next ctx
Run Code Online (Sandbox Code Playgroud)