Skip to content

Commit

Permalink
api: refactor listdid feature components to use BaseGatewayEndpointError
Browse files Browse the repository at this point in the history
  • Loading branch information
maany committed Jul 17, 2023
1 parent 1620c3a commit 11b5677
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 42 deletions.
21 changes: 4 additions & 17 deletions src/lib/core/dto/did-dto.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
import { BaseDTO, BaseHttpErrorTypes, BaseStreamableDTO } from '@/lib/sdk/dto'
import { BaseDTO, BaseStreamableDTO } from '@/lib/sdk/dto'
import { DID, DIDMeta } from '@/lib/core/entity/rucio'

/**
* Data Transfer Object for ListDIDsEndpoint
*/
export interface ListDIDDTO extends BaseStreamableDTO {
error?:
| 'Invalid Auth Token'
| 'Invalid key in filter'
| 'Not acceptable'
| 'Wrong did type'
| 'Unknown Error'
}
export interface ListDIDDTO extends BaseStreamableDTO {}

/**
* Represents the individual data items in the stream
Expand All @@ -20,15 +14,8 @@ export type ListDIDsStreamData = string
/**
* Data Transfer Object for GetDIDEndpoint
*/
export interface DIDDTO extends DID {
export interface DIDDTO extends DID, BaseDTO {
status: 'success' | 'error'
error:
| 'Invalid Auth Token'
| 'Data Identifier Not Found'
| 'Invalid Parameters'
| 'Scope Not Found'
| 'Unknown Error'
| null
message?: string
account: string
open: boolean
Expand Down
6 changes: 3 additions & 3 deletions src/lib/core/use-case/list-dids/list-dids-usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class ListDIDsUseCase extends BaseMultiCallStreamableUseCase<ListDIDsRequest, Li

handleGatewayError(error: ListDIDDTO): ListDIDsError {
let errorType = 'Unknown Error'
if(error.error === 'Invalid Auth Token') {
let message = error.error?.errorMessage
if(message === 'Invalid Auth Token') {
errorType = 'Invalid Request'
}
else if(error.error !== 'Unknown Error') {
else if(message !== 'Unknown Error') {
errorType = 'Invalid DID Query'
}

return {
error: errorType,
message: `${error.error}: ${error.message}`,
Expand Down
7 changes: 5 additions & 2 deletions src/lib/core/use-case/list-dids/pipeline-element-get-did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ export default class GetDIDsPipelineElement extends BaseStreamingPostProcessingP
} catch (error: any) {
const errorDTO: DIDDTO = {
status: 'error',
error: 'Invalid Parameters',
error: {
errorMessage: 'Invalid Parameters',
errorCode: 400
},
message: (error as Error).message,
name: requestModel.query,
scope: requestModel.query,
Expand All @@ -35,7 +38,7 @@ export default class GetDIDsPipelineElement extends BaseStreamingPostProcessingP

handleGatewayError(dto: DIDDTO): ListDIDsError {
let error: 'Unknown Error' | 'Invalid DID Query' | 'Invalid Request' = 'Unknown Error';
switch(dto.error) {
switch(dto.error?.errorMessage) {
case 'Invalid Auth Token':
error = 'Invalid Request';
break;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/infrastructure/gateway/did-gateway/did-gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export default class RucioDIDGateway implements DIDGatewayOutputPort {
} catch (error) {
const errorDTO: ListDIDDTO = {
status: 'error',
error: 'Unknown Error',
error: BaseHttpErrorTypes.UNKNOWN_ERROR,
message: error?.toString(),
}
return Promise.resolve(errorDTO)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseEndpoint } from "@/lib/sdk/gateway-endpoints"
import { BaseEndpoint, BaseHttpErrorTypes } from "@/lib/sdk/gateway-endpoints"
import { HTTPRequest } from "@/lib/common/http";
import { DIDDTO } from "@/lib/core/dto/did-dto";
import { DIDType } from "@/lib/core/entity/rucio";
Expand Down Expand Up @@ -38,7 +38,7 @@ export default class GetDIDEndpoint extends BaseEndpoint<DIDDTO> {
async reportErrors(statusCode: number, response: Response): Promise<DIDDTO | undefined> {
const dto: DIDDTO = {
status: 'error',
error: 'Unknown Error',
error: BaseHttpErrorTypes.UNKNOWN_ERROR,
name: this.name,
scope: this.scope,
did_type: DIDType.UNKNOWN,
Expand All @@ -49,21 +49,26 @@ export default class GetDIDEndpoint extends BaseEndpoint<DIDDTO> {
bytes: 0,
length: 0,
}
let error = {
errorMessage: '',
errorCode: statusCode
}
dto.error = error
switch (statusCode) {
case 400:
dto.error = 'Invalid Parameters'
dto.error.errorMessage = 'Invalid Parameters'
break;
case 404:
const error = await response.json()
if (error.exception === 'DataIdentifierNotFound') {
dto.error = 'Data Identifier Not Found'
dto.error.errorMessage = 'Data Identifier Not Found'
}
if (error.exception === 'ScopeNotFound') {
dto.error = 'Scope Not Found'
dto.error.errorMessage = 'Scope Not Found'
}
break
default:
dto.error = 'Unknown Error'
dto.error.errorMessage = 'Unknown Error'
break
}
return dto
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BaseStreamableEndpoint } from "@/lib/sdk/gateway-endpoints"
import { BaseHttpErrorTypes, BaseStreamableEndpoint } from "@/lib/sdk/gateway-endpoints"
import { HTTPRequest } from "@/lib/common/http"
import { ListDIDDTO } from "@/lib/core/dto/did-dto"
import { DID, DIDType } from "@/lib/core/entity/rucio"
Expand Down Expand Up @@ -57,24 +57,18 @@ export default class ListDIDsEndpoint extends BaseStreamableEndpoint<ListDIDDTO,

const dto: ListDIDDTO = {
status: 'error',
error: 'Unknown Error',
error: BaseHttpErrorTypes.UNKNOWN_ERROR,
stream: null,
}
switch (statusCode) {
case 401:
dto.error = 'Invalid Auth Token'
break
case 404:
dto.error = 'Invalid key in filter'
break
case 406:
dto.error = 'Not acceptable'
break
case 409:
dto.error = 'Wrong did type'
dto.error = {
errorCode: 409,
errorMessage: 'Wrong DID type',
}
break
default:
dto.error = 'Unknown Error'
dto.error = BaseHttpErrorTypes.UNKNOWN_ERROR
break
}
return Promise.resolve(dto)
Expand Down

0 comments on commit 11b5677

Please sign in to comment.