Skip to content

Commit

Permalink
chore: improve error output
Browse files Browse the repository at this point in the history
  • Loading branch information
psanders committed Jan 29, 2025
1 parent c8d479d commit d20a4c6
Show file tree
Hide file tree
Showing 12 changed files with 546 additions and 288 deletions.
30 changes: 24 additions & 6 deletions mods/common/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class ServiceDefinitionNotFoundError extends Error {
* @param {string} version - The version of the service definition.
*/
constructor(name: string, version: string) {
super(`service definition for ${name}/${version} not found`)
super(`Service definition for ${name}/${version} not found`)
// Set the prototype explicitly.
Object.setPrototypeOf(this, ServiceDefinitionNotFoundError.prototype)
}
Expand All @@ -47,7 +47,7 @@ export class ServiceUnavailableError extends Error {
* @param {string} address - The address of the service.
*/
constructor(address: string) {
super(`service unavailable [service address = ${address}]`)
super(`Service unavailable [service address = ${address}]`)
this.code = grpc.status.UNAVAILABLE
Object.setPrototypeOf(this, ServiceUnavailableError.prototype)
}
Expand All @@ -64,7 +64,7 @@ export class ClientConnectionError extends Error {
*/
constructor(address: string, isSecure: boolean) {
super(
`cannot connect to service [service address = ${address}, tlsOn = ${isSecure}]`
`Cannot connect to service [service address = ${address}, tlsOn = ${isSecure}]`
)
}
}
Expand All @@ -80,7 +80,7 @@ export class UnimplementedError extends Error {
*/
constructor() {
super(
"this operation is not supported/enabled in this data api implementation."
"This operation is not supported/enabled in this data api implementation."
)
this.code = grpc.status.UNIMPLEMENTED
Object.setPrototypeOf(this, UnimplementedError.prototype)
Expand All @@ -99,12 +99,30 @@ export class ResourceNotFoundError extends Error {
* @param {string} ref - the reference of the resource
*/
constructor(ref: string) {
super(`resource not found: ${ref}`)
super(`Resource not found: ${ref}`)
this.code = grpc.status.NOT_FOUND
Object.setPrototypeOf(this, ResourceNotFoundError.prototype)
}
}

/**
* Thrown if the resource already exists.
*/
export class ResourceAlreadyExistsError extends Error {
code: number

/**
* Creates an instance of ResourceAlreadyExistsError.
*
* @param {string} message - message with the error to be thrown.
*/
constructor(message?: string) {
super(message ?? "Resource already exists")
this.code = grpc.status.ALREADY_EXISTS
Object.setPrototypeOf(this, ResourceAlreadyExistsError.prototype)
}
}

/**
* Thrown if the request is invalid.
*/
Expand All @@ -117,7 +135,7 @@ export class BadRequestError extends Error {
* @param {string} message - optional message with the error. Defaults to "bad request."
*/
constructor(message?: string) {
super(message ?? "bad request")
super(message ?? "Bad request")
this.code = grpc.status.INVALID_ARGUMENT
Object.setPrototypeOf(this, BadRequestError.prototype)
}
Expand Down
2 changes: 1 addition & 1 deletion mods/common/test/common.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ describe("@routr/common", () => {
// Introduce a typo
objectProto2.name = "processo"
expect(getObjectProto(objectProto2).toString()).to.include(
"service definition for processo/v2beta1 not found"
"Service definition for processo/v2beta1 not found"
)
})

Expand Down
8 changes: 4 additions & 4 deletions mods/pgdata/src/api/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ export function create(
callback(
{
code: grpc.status.UNAVAILABLE,
message: "database is not available"
message: "Service unavailable"
},
null
)
return
} else if (e.code === "P2002") {
callback(
new CE.BadRequestError(
"entity already exist for field: " + e.meta.target[0]
new CE.ResourceAlreadyExistsError(
"Resource already exist for field: " + e.meta.target[0]
),
null
)
} else if (e.code === "P2003") {
callback(
new CE.BadRequestError(
"dependent entity doesn't exist for: " + e.meta.field_name
"Dependent resource doesn't exist for: " + e.meta.field_name
),
null
)
Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/api/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function del(operation: PrismaOperation) {
callback(
{
code: grpc.status.UNAVAILABLE,
message: "database is not available"
message: "Service unavailable"
},
null
)
Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/api/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export function findBy(
callback(
{
code: grpc.status.UNAVAILABLE,
message: "database is not available"
message: "Service unavailable"
},
null
)
Expand Down
6 changes: 3 additions & 3 deletions mods/pgdata/src/api/get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import { PrismaClientInitializationError } from "@prisma/client/runtime/library"
export function get(operation: PrismaOperation, kind: CC.KindWithoutUnknown) {
return async (call: CT.GrpcCall, callback: CT.GrpcCallback) => {
if (!call.request.ref) {
return callback(new CE.BadRequestError("parameter ref is required"), null)
return callback(new CE.BadRequestError("Parameter ref is required"), null)
}

const Manager = getManager(kind)
Expand All @@ -58,7 +58,7 @@ export function get(operation: PrismaOperation, kind: CC.KindWithoutUnknown) {
callback(
{
code: grpc.status.UNAVAILABLE,
message: "database is not available"
message: "Service unavailable"
},
null
)
Expand All @@ -68,7 +68,7 @@ export function get(operation: PrismaOperation, kind: CC.KindWithoutUnknown) {
callback(
{
code: grpc.status.UNKNOWN,
message: "unknown database error"
message: "Unknown error"
},
null
)
Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/api/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export function list(
callback(
{
code: grpc.status.UNAVAILABLE,
message: "database is not available"
message: "Service unavailable"
},
null
)
Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/api/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function update(
} else if (e.code === "P2003") {
callback(
new CE.BadRequestError(
"dependent entity doesn't exist for: " + e.meta.field_name
"Dependent resource doesn't exist for: " + e.meta.field_name
),
null
)
Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/mappers/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type AgentWithDomainAndCredentials = Prisma.AgentGetPayload<{

// Needs testing
export class AgentManager extends EntityManager {
constructor(private agent: CC.Agent) {
constructor(private readonly agent: CC.Agent) {
super()
}

Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/mappers/credentials.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { JsonValue } from "@prisma/client/runtime/library"

// Needs testing
export class CredentialsManager extends EntityManager {
constructor(private credentials: CC.Credentials) {
constructor(private readonly credentials: CC.Credentials) {
super()
}

Expand Down
2 changes: 1 addition & 1 deletion mods/pgdata/src/mappers/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type DomainWithACL = Prisma.DomainGetPayload<{

// Needs testing
export class DomainManager extends EntityManager {
constructor(private domain: CC.Domain) {
constructor(private readonly domain: CC.Domain) {
super()
}

Expand Down
Loading

0 comments on commit d20a4c6

Please sign in to comment.