Skip to content

Improved errorhandling #1140

Closed
Closed

Description

@maarre

It took a while to figure out how to convert this exception to normal control flow. Typescript adds some extra difficulty. It is pretty ugly....

    catch ( e ) {
        if ( Object.prototype.toString.call(e) === '[object Error]') {
            const e2 = e as Error
            if ( e2['code' as keyof typeof e2 ] === '22003'  ) {
                const detail: string | unknown = e2['detail' as keyof typeof e2]
                if ( detail ) {
                    logger.debug(`detail=${JSON.stringify(detail)}`)
                    return new UserError(detail.toString())
                }
                else {
                    logger.error(`sp_breedsimm_create_v1: Caught and rethrowing ${JSON.stringify(e2)} for indata ${JSON.stringify(params, undef_replacer)}`)
                    throw e
                }
            }
            else {
                logger.error(`sp_breedsimm_create_v1: Caught and rethrowing ${JSON.stringify(e2)} for indata ${JSON.stringify(params, undef_replacer)}`)
                throw e
            }
        }
        else {
            logger.error(`sp_breedsimm_create_v1: Caught and rethrowing ${JSON.stringify(e)} of type ${Object.prototype.toString.call(e)} for indata ${JSON.stringify(params, undef_replacer)}`)
            throw e
        }

If a new class with a constructor is used maybe the above code could be converted to :

    catch ( e ) {
            const e2 = e as PostgresError
            if ( e2.code === '22003'  ) {
                const = e2.detail
                logger.debug(`detail=${JSON.stringify(detail)}`)
                return new UserError(detail.toString())
            }
            else {
                logger.error(`sp_breedsimm_create_v1: Caught and rethrowing ${JSON.stringify(e2)} for indata ${JSON.stringify(params, undef_replacer)}`)
                throw e
            }

While you are at it maybe even catergorize the errors? Separate classes for where or how the error can be solved, or a new property. I propose these categories, programming, configuration, retryable, user. A programming error must be solved by changing the code. Configuration should be solved by changing the configuration of the deployment. Retryable is typcally solved by retrying after a delay. User is is solved by supplying different indata.

Activity

  1. hilja commented on Dec 15, 2025

    @hilja

    Drizzle has some jabber on this.

    The linked example uses pg but it's not far from postgres.js. Some sort of mapping/error getter would be neat.

  2. maarre commented on Jan 16, 2026

    @maarre
    Author

    I have some success at my end regarding error handling. No need to improve code, but maybe some improved documentation?

    This is my current typescript code:

    export function get_type(obj: any) : string {
        const base_type: string = obj ? typeof obj : 'null'
        const type = base_type === 'object' ? obj.constructor.name : base_type
        return type
    }
    
    export class UserError {
        message: string
        constructor(message: string) {
            this.message = message
        }
    }
    
    export function get_user_error(e: any) : UserError | null {
        const type = get_type(e)
        if ( type === 'PostgresError') {
            const e2 = e as PostgresError
            if ( e2.code === '22003' || e2.code === '22P02' ) {
                const base = e2.toString().replace('PostgresError: ', '')
                const detail = e2.detail
                return new UserError(detail ? detail : base.charAt(0).toUpperCase() + base.slice(1))
            }
        }
        return null
    }
    
    catch ( e ) {
            const user_error = get_user_error(e)
            if ( user_error ) {
                return user_error
            }
            else {
                logger.error(`: Caught and rethrowing ${JSON.stringify(e)} for indata ${JSON.stringify(params, undef_replacer)}. Stack=${(e as Error).stack}`)
                throw e
            }
    
        }
    
  3. porsager commented on Oct 29, 2026

    @porsager
    Owner

    v4 documents errors: every server error is a PostgresError, exported by name, with the server's fields (code, detail, constraint_name and so on) as typed properties. See Error handling:

    import { PostgresError } from 'postgres'
    
    if (error instanceof PostgresError && error.code === '22003')
      console.log(error.detail)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions