dotnet / dotnet/SqlClient

[Proposal] It should be possible to manually create SqlException

Open
#35 2 comments 4 reactions 0 assignees View on GitHub
Approved
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 19h
Merged PRs (30d)
72

Description

`SqlException` create methods are currently internal.
I would like some kind of create method to be public.

## Rationale

Unit tests. There are various scenarios in which SqlException is handled differently than other kinds of exceptions, and being able to test those scenarios without actual database would be useful.

Related StackOverflow thread, [How to throw a SqlException when needed for mocking and unit testing?](https://stackoverflow.com/questions/1386962/how-to-throw-a-sqlexception-when-needed-for-mocking-and-unit-testing), has 53 upvotes.

## Potential API

```
class SqlException {
// ...
public SqlException(
string message,
SqlErrorCollection errorCollection,
Guid clientConnectionId = Guid.Empty,
Exception innerException = null
) { ... }
}

class SqlErrorCollection {
// ...
public SqlErrorCollection() { ... }
public SqlErrorCollection(params SqlError[] errors) { ... }
}

class SqlError {
// ...
public SqlError(
int infoNumber,
byte errorState,
byte errorClass,
string server,
string errorMessage,
string procedure,
int lineNumber
) { ... }
}
```

## Open Questions
Is there a better way to expose `SqlError` that would allow easier future extensibility?
Some kind of `SqlErrorBuilder`?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.