porsager / porsager/postgres

Improved errorhandling

Open
#1,140 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

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.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start from the catch block shown in the issue and the PostgreSQL error code 22003 handling around sp_breedsimm_create_v1. Determine whether the proposed PostgresError class and programming, configuration, retryable, and user categories fit the project's existing error behavior; done requires an agreed design and corresponding coverage for the affected error paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, postgresql, typescript
Domain
backend, database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.