How to use the @poppinss/utils.Exception function in @poppinss/utils

To help you get started, we’ve selected a few @poppinss/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adonisjs / adonis-lucid / src / Orm / Preloader / index.ts View on Github external
public parseRelationName (relationName: string) {
    const relations = relationName.split('.')
    const primary = relations.shift()!
    const relation = this._model.$getRelation(primary)

    /**
     * Undefined relationship
     */
    if (!relation) {
      throw new Exception(
        `${primary} is not defined as a relationship on ${this._model.name} model`,
        500,
        'E_UNDEFINED_RELATIONSHIP',
      )
    }

    return {
      primary,
      relation,
      children: relations.length ? { relationName: relations.join('') } : null,
    }
  }
github adonisjs / adonis-lucid / src / Database / QueryBuilder / Database.ts View on Github external
private _ensureCanPerformWrites () {
    if (this.client && this.client.mode === 'read') {
      throw new Exception('Updates and deletes cannot be performed in read mode')
    }
  }
github adonisjs / adonis-lucid / src / Orm / BaseModel / index.ts View on Github external
const Model = this.constructor as typeof BaseModel
    const relation = Model.$relations.get(key as string)

    /**
     * Ignore when relation is not defined
     */
    if (!relation) {
      return
    }

    /**
     * Reset array before invoking $pushRelated
     */
    if (MANY_RELATIONS.includes(relation.type)) {
      if (!Array.isArray(models)) {
        throw new Exception(
          `${Model.name}.${key} must be an array when setting ${relation.type} relationship`,
        )
      }
      this.$preloaded[key] = []
    }

    return this.$pushRelated(key, models)
  }
github adonisjs / adonis-lucid / src / Orm / BaseModel / index.ts View on Github external
private _ensureIsntDeleted () {
    if (this.$isDeleted) {
      throw new Exception('Cannot mutate delete model instance', 500, 'E_MODEL_DELETED')
    }
  }
github edge-js / edge / src / Loader / index.ts View on Github external
public makePath (templatePath: string): string {
    if (this._preRegistered.has(templatePath)) {
      return templatePath
    }

    const [ diskName, template ] = extractDiskAndTemplateName(templatePath)

    /**
     * Raise exception when disk name is not defined
     */
    const mountedDir = this._mountedDirs.get(diskName)
    if (!mountedDir) {
      throw new Exception(`{${diskName}} namespace is not mounted`, 500, 'E_UNMOUNTED_DISK_NAME')
    }

    return join(mountedDir, template)
  }
github adonisjs / adonis-lucid / src / Orm / Relations / HasManyThrough / index.ts View on Github external
private _validateKeys () {
    const relationRef = `${this.model.name}.${this.relationName}`

    if (!this.model.$hasColumn(this.localKey)) {
      const ref = `${this.model.name}.${this.localKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_RELATED_LOCAL_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.foreignKey)) {
      const ref = `${this.throughModel().name}.${this.foreignKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_RELATED_FOREIGN_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.throughLocalKey)) {
      const ref = `${this.throughModel().name}.${this.throughLocalKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_THROUGH_LOCAL_KEY',
      )
    }

    if (!this.relatedModel().$hasColumn(this.throughForeignKey)) {
github adonisjs / adonis-lucid / src / Orm / Relations / ManyToMany / index.ts View on Github external
private _ensureRelatedModel () {
    if (!this._options.relatedModel) {
      throw new Exception(
        'Related model reference is required to construct the relationship',
        500,
        'E_MISSING_RELATED_MODEL',
      )
    }
  }
github adonisjs / adonis-lucid / src / utils / index.ts View on Github external
export function ensureRelation (
  name: string,
  relation?: T,
): relation is T {
  if (!relation) {
    throw new Exception(`Cannot process unregistered relationship ${name}`, 500)
  }

  return true
}
github adonisjs / adonis-lucid / src / Orm / Relations / HasManyThrough / index.ts View on Github external
'E_MISSING_RELATED_LOCAL_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.foreignKey)) {
      const ref = `${this.throughModel().name}.${this.foreignKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_RELATED_FOREIGN_KEY',
      )
    }

    if (!this.throughModel().$hasColumn(this.throughLocalKey)) {
      const ref = `${this.throughModel().name}.${this.throughLocalKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_THROUGH_LOCAL_KEY',
      )
    }

    if (!this.relatedModel().$hasColumn(this.throughForeignKey)) {
      const ref = `${this.relatedModel().name}.${this.throughForeignKey}`
      throw new Exception(
        `${ref} required by ${relationRef} relation is missing`,
        500,
        'E_MISSING_THROUGH_FOREIGN_KEY',
      )
    }
  }
github edge-js / edge / src / Loader / index.ts View on Github external
/**
     * Ensure template content is defined as a string
     */
    if (typeof (contents.template) !== 'string') {
      throw new Exception(
        'Make sure to define the template content as a string',
        500,
        'E_MISSING_TEMPLATE_CONTENTS',
      )
    }

    /**
     * Do not overwrite existing template with same template path
     */
    if (this._preRegistered.has(templatePath)) {
      throw new Exception(
        `Cannot override previously registered {${templatePath}} template`,
        500,
        'E_DUPLICATE_TEMPLATE_PATH',
      )
    }

    this._preRegistered.set(templatePath, contents)
  }
}

@poppinss/utils

Handy utilities for repetitive work

MIT
Latest version published 4 months ago

Package Health Score

79 / 100
Full package analysis