Skip to content

Commit 9f76281

Browse files
committed
fixes error handling
1 parent 877141a commit 9f76281

4 files changed

Lines changed: 16 additions & 24 deletions

File tree

src/dataverseClient.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DatasetUtil } from './utils/datasetUtil'
88
import request from 'request-promise'
99
import { DatasetVersionUpgradeType } from './@types/datasetVersionUpgradeType'
1010
import { DatasetVersionType } from './@types/datasetVersionType'
11-
import { ResponseUtil } from './utils/ResponseUtil'
11+
import { ResponseUtil } from "./utils/ResponseUtil"
1212

1313
export class DataverseClient {
1414
private readonly host: string
@@ -121,7 +121,7 @@ export class DataverseClient {
121121
}
122122

123123
return request.post(options).catch(error => {
124-
throw new DataverseException(error.response.statusCode, ResponseUtil.getErrorMessage(error.response.body))
124+
throw new DataverseException(error.response.statusCode, ResponseUtil.getErrorBody(error.response.body))
125125
})
126126
}
127127

@@ -164,7 +164,7 @@ export class DataverseClient {
164164
}
165165

166166
return request.post(options).catch(error => {
167-
throw new DataverseException(error.response.statusCode, ResponseUtil.getErrorMessage(error.response.body))
167+
throw new DataverseException(error.response.statusCode, ResponseUtil.getErrorBody(error.response.body))
168168
})
169169
}
170170

src/utils/ResponseUtil.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
export class ResponseUtil {
2-
public static getErrorMessage(body: string): string {
3-
let message: string
4-
try {
5-
message = JSON.parse(body).message
6-
}
7-
catch {
8-
message = ''
9-
}
10-
11-
return message
2+
public static getErrorBody(errorBody: string): string {
3+
return errorBody ? errorBody : ''
124
}
135
}

test/dataverseClient.spec.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ describe('DataverseClient', () => {
3030
let requestPostStub: SinonStub
3131

3232
let mapBasicDatasetInformationStub: SinonStub
33-
let getErrorMessageStub: SinonStub
33+
let getErrorBodyStub: SinonStub
3434

3535
beforeEach(() => {
3636
apiToken = random.uuid()
@@ -65,7 +65,7 @@ describe('DataverseClient', () => {
6565
requestPostStub = sandbox.stub(request, 'post').resolves(mockResponse)
6666

6767
mapBasicDatasetInformationStub = sandbox.stub(DatasetUtil, 'mapBasicDatasetInformation').returns(mockDatasetInformation)
68-
getErrorMessageStub = sandbox.stub(ResponseUtil, 'getErrorMessage').returns(mockErrorMessage)
68+
getErrorBodyStub = sandbox.stub(ResponseUtil, 'getErrorBody').returns(mockErrorMessage)
6969
})
7070

7171
afterEach(() => {
@@ -981,14 +981,14 @@ describe('DataverseClient', () => {
981981
})
982982
})
983983

984-
it('should call getErrorMessage', async () => {
984+
it('should call getErrorBody', async () => {
985985
let error: DataverseException = undefined
986986

987987
await client.uploadDatasetThumbnail(datasetId, imageBuffer).catch(e => error = e)
988988

989989
expect(error).to.be.instanceOf(Error)
990-
assert.calledOnce(getErrorMessageStub)
991-
assert.calledWithExactly(getErrorMessageStub, mockBody)
990+
assert.calledOnce(getErrorBodyStub)
991+
assert.calledWithExactly(getErrorBodyStub, mockBody)
992992
})
993993

994994
it('should throw expected error', async () => {
@@ -1282,14 +1282,14 @@ describe('DataverseClient', () => {
12821282
})
12831283
})
12841284

1285-
it('should call getErrorMessage', async () => {
1285+
it('should call getErrorBody', async () => {
12861286
let error: DataverseException = undefined
12871287

12881288
await client.replaceFile(mockFileId, mockFilename, fileBuffer).catch(e => error = e)
12891289

12901290
expect(error).to.be.instanceOf(Error)
1291-
assert.calledOnce(getErrorMessageStub)
1292-
assert.calledWithExactly(getErrorMessageStub, mockBody)
1291+
assert.calledOnce(getErrorBodyStub)
1292+
assert.calledWithExactly(getErrorBodyStub, mockBody)
12931293
})
12941294

12951295
it('should throw expected error', async () => {

test/utils/ResponseUtil.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ import { ResponseUtil } from '../../src/utils/ResponseUtil'
22
import { random } from 'faker'
33

44
describe('ResponseUtil', () => {
5-
describe('getErrorMessage', () => {
5+
describe('getErrorBody', () => {
66
let mockMessage: string
77

88
beforeEach(() => {
99
mockMessage = random.words()
1010
})
1111

1212
it('should return expected message', () => {
13-
const expectedResult: string = mockMessage
13+
const expectedResult: string = JSON.stringify({ message: mockMessage })
1414

15-
const result: string = ResponseUtil.getErrorMessage(JSON.stringify({ message: mockMessage }))
15+
const result: string = ResponseUtil.getErrorBody(JSON.stringify({ message: mockMessage }))
1616

1717
expect(result).toEqual(expectedResult)
1818
})

0 commit comments

Comments
 (0)