Skip to content

Commit

Permalink
SDK-3306: Error class hierarchy
Browse files Browse the repository at this point in the history
This introduces the core classes for this library, namely:
- `InruptClientError`, the superclass for all Inrupt client related errors,
-  `ClientHttpError`, the base class for HTTP errors

Note that classes for specific HTTP errors (Unauthenticated, Not Found...) are not included yet, and will be added later.

`ClientHttpError` extends the base `InruptClientError` by implementing two interfaces, `WithProblemDetails` and `WithErrorResponse`, respectively associated to their type guards `hasProblemDetails` and `hasErrorResponse`. Both the `.problemDetails` and `.response` accessors are marked as read-only, and immutability is enforced at runtime for libraries not using static analysis based on TS.

The constructor of `ClientHttpError` takes in a response body and a subset of the native `Response` metadata, instead of taking a simple `Response` instance, because reading the `Response` body is an asynchronous operation, which isn't possible when building an object. It is expected that the parsing will be done by the caller in an async context, and the result will be passed to the `ClientHttpError` constructor, which is not the initial design.

For the time being, I'm not doing any submodule exports (e.g. `@inrupt/solid-client-error/http`), but this is something we might eventually consider for tree-shaking.
  • Loading branch information
NSeydoux committed Jul 1, 2024
1 parent 3954f9f commit 250ae7f
Show file tree
Hide file tree
Showing 21 changed files with 1,141 additions and 21 deletions.
19 changes: 18 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,22 @@ module.exports = {
parserOptions: {
project: "./tsconfig.eslint.json",
},
ignorePatterns: ["dist/"],
overrides: [
{
rules: {
// Conflicts with TS imports
"import/no-unresolved": "off",
"no-shadow": [
"error",
{
// status is a (deprecated) global variable, but it also is the
// conventional name for a Response attribute.
allow: ["status"],
},
],
},
files: "*",
},
],
ignorePatterns: ["dist/", "docs/"],
};
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,5 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

docs/api/source/
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Solid JavaScript Error - solid-client-errors
# Solid JavaScript Error - solid-client-error

[![Contributor
Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE-OF-CONDUCT.md)
Expand All @@ -8,7 +8,7 @@ conduct](CODE-OF-CONDUCT.md). By participating, you are expected to uphold this
code. Please report unacceptable behavior to
[[email protected]](mailto:[email protected]).

`@inrupt/solid-client-errors` is a JavaScript library for handling [RFC9457 Problem Details](https://www.rfc-editor.org/rfc/rfc9457) on HTTP error responses.
`@inrupt/solid-client-error` is a JavaScript library for handling [RFC9457 Problem Details](https://www.rfc-editor.org/rfc/rfc9457) on HTTP error responses.

# Server support

Expand Down Expand Up @@ -43,7 +43,7 @@ releases](https://nodejs.org/en/about/releases/), and support 18.x and 20.x.
For the latest stable version of solid-client-error:

```bash
npm install @inrupt/solid-client-errors
npm install @inrupt/solid-client-error
```

# Issues & Help
Expand Down
3 changes: 3 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ import type { Config } from "jest";
type ArrayElement<MyArray> = MyArray extends Array<infer T> ? T : never;

const baseConfig: ArrayElement<NonNullable<Config["projects"]>> = {
coveragePathIgnorePatterns: [".*\\.mock\\.ts"],
modulePathIgnorePatterns: ["dist/", "<rootDir>/examples/"],
// Setup required because of missing globals in JSDom
setupFilesAfterEnv: ["<rootDir>/jest.setup.ts"],
testRegex: "/src/.*\\.test\\.ts$",
clearMocks: true,
injectGlobals: false,
Expand Down
22 changes: 22 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// Copyright Inrupt Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
// Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
// SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

import "@inrupt/jest-jsdom-polyfills";
Loading

0 comments on commit 250ae7f

Please sign in to comment.