Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Enhance error middleware #261

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ trash

\.vscode/
\.vs/
\.idea/

# confluent-schema-registry
#
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
## [3.3.1] - 2024-05-30

### Fixed
- Exclude `.idea` folder from git vcs.
- Show client-side errors when the error data is an object [#261](https://github.com/kafkajs/confluent-schema-registry/pull/261)

## [3.3.0] - 2022-10-04

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kafkajs/confluent-schema-registry",
"version": "3.3.0",
"version": "3.3.1",
"main": "dist/index.js",
"description": "ConfluentSchemaRegistry is a library that makes it easier to interact with the Confluent schema registry, it provides convenient methods to encode, decode and register new schemas using the Apache Avro serialization format.",
"keywords": [
Expand Down
12 changes: 12 additions & 0 deletions src/api/middleware/errorMiddleware.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ describe('ErrorMiddleware', () => {
)
})

it('raises an error with a message in case of client-side object error', async () => {
const message = { cause: 'unknown cause', metadata: { id: 1 } }
const response = createResponse(message)

await expect(
executedMiddleware.response(() => Promise.reject(response), undefined),
).rejects.toHaveProperty(
'message',
`${middlewareParams.clientId} - Error, status 500: ${JSON.stringify(message)}`,
)
})

it('raise an error with a default message if the error payload is empty', async () => {
const response = createResponse('')

Expand Down
10 changes: 8 additions & 2 deletions src/api/middleware/errorMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@ class ResponseError extends Error {
url: string

constructor(clientName: string, response: ConfluenceResponse) {
const responseData = response.data()
const isResponseDataAnObject = typeof responseData === 'object'
const responseDataString = isResponseDataAnObject ? JSON.stringify(responseData) : responseData

super(
`${clientName} - ${response.data().message ||
`Error, status ${response.status()}${response.data() ? `: ${response.data()}` : ''}`}`,
`${clientName} - ${responseData.message ||
`Error, status ${response.status()}${
responseDataString ? `: ${responseDataString}` : ''
}`}`,
)

const request = response.request()
Expand Down