Skip to content

Commit

Permalink
fix: throw descriptive errors
Browse files Browse the repository at this point in the history
  • Loading branch information
tale committed Feb 22, 2022
1 parent 9e24787 commit 9dd959a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 25 deletions.
18 changes: 17 additions & 1 deletion src/base.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { MissingRequiredKeyError } from "./error"

export class CaseCopyMap extends Map<string, string> {
set(key: string, value: string): this {
super.set(`case_copying${key.toLowerCase()}`, key)
Expand All @@ -24,12 +26,26 @@ export class APTBase {
*/
protected raw: CaseCopyMap


/**
* Raw-accessible list of the required APT keys
*/
protected required: string[]

/**
* Base constructor.
* This should never be used.
*/
constructor() {
constructor(required: string[]) {
this.raw = new CaseCopyMap()
this.required = required

for (const key of this.required) {
if (!this.raw.has(key)) {
throw new MissingRequiredKeyError(key)
}
}

}

/**
Expand Down
52 changes: 29 additions & 23 deletions src/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,35 +234,41 @@ export interface IBinaryControl {
*/
export class BinaryControl extends APTBase implements IBinaryControl {
// Begin Raw Implementation
package: string
source?: string | undefined
version: string
section?: string | undefined
priority?: PriorityLevel | undefined
architecture: string
essential?: boolean | undefined
depends?: string[] | undefined
preDepends?: string[] | undefined
recommends?: string[] | undefined
suggests?: string[] | undefined
replaces?: string[] | undefined
enhances?: string[] | undefined
breaks?: string[] | undefined
conflicts?: string[] | undefined
installedSize?: number | undefined
maintainer: string
description: string
homepage?: string | undefined
builtUsing?: string | undefined
packageType?: PackageType | undefined
package: string
source?: string | undefined
version: string
section?: string | undefined
priority?: PriorityLevel | undefined
architecture: string
essential?: boolean | undefined
depends?: string[] | undefined
preDepends?: string[] | undefined
recommends?: string[] | undefined
suggests?: string[] | undefined
replaces?: string[] | undefined
enhances?: string[] | undefined
breaks?: string[] | undefined
conflicts?: string[] | undefined
installedSize?: number | undefined
maintainer: string
description: string
homepage?: string | undefined
builtUsing?: string | undefined
packageType?: PackageType | undefined
// End Raw Implementation

/**
* Create a type-safe Control object and populate its keys
* @param {string} rawData Contents of a control file from a debian binary
*/
constructor(rawData: string) {
super()
constructor(rawData: string) {
super([
'Package',
'Version',
'Architecture',
'Maintainer',
'Description'
])

const map = parseKV(rawData)
this.raw = map
Expand Down
8 changes: 8 additions & 0 deletions src/error.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export class MissingRequiredKeyError extends Error {
constructor(key: string) {
super()

this.name = 'MissingRequiredKeyError'
this.message = `Missing Required APT Key: ${key}`
}
}
4 changes: 4 additions & 0 deletions src/packages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ export class Package extends BinaryControl implements IPackage {
*/
constructor(rawData: string) {
super(rawData)
super.required = [
'Filename',
'Size'
]

this.filename = this.raw.get('Filename')!.trim()
this.size = parseFloat(this.raw.get('Size')!.trim())
Expand Down
5 changes: 4 additions & 1 deletion src/release.ts
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ export class Release extends APTBase implements IRelease {
* @param {string} rawData Contents of a Release file from an APT repository
*/
constructor(rawData: string) {
super()
super([
'Architectures',
'Components'
])

const map = parseKV(rawData)
this.raw = map
Expand Down

0 comments on commit 9dd959a

Please sign in to comment.