This repository has been archived by the owner on Mar 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from mazgi/improve-error-handling
Improve error handling
- Loading branch information
Showing
41 changed files
with
606 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,7 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class Initialize1580144828876 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<any> {} | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,20 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class CreateTheWorld1580144854019 implements MigrationInterface { | ||
name = 'CreateTheWorld1580144854019' | ||
name = 'CreateTheWorld1580144854019' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query("CREATE TABLE `world` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, UNIQUE INDEX `IDX_9aa9325530beea5c1fdf1791f3` (`name`), PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query("DROP INDEX `IDX_9aa9325530beea5c1fdf1791f3` ON `world`", undefined); | ||
await queryRunner.query("DROP TABLE `world`", undefined); | ||
} | ||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
'CREATE TABLE `world` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, UNIQUE INDEX `IDX_9aa9325530beea5c1fdf1791f3` (`name`), PRIMARY KEY (`id`)) ENGINE=InnoDB', | ||
undefined | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
'DROP INDEX `IDX_9aa9325530beea5c1fdf1791f3` ON `world`', | ||
undefined | ||
) | ||
await queryRunner.query('DROP TABLE `world`', undefined) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,28 @@ | ||
import {MigrationInterface, QueryRunner} from "typeorm"; | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class CreateUser1580296463597 implements MigrationInterface { | ||
name = 'CreateUser1580296463597' | ||
name = 'CreateUser1580296463597' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query("CREATE TABLE `user` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `displayName` varchar(255) NOT NULL, `hashedPassword` varchar(255) NULL, UNIQUE INDEX `IDX_065d4d8f3b5adb4a08841eae3c` (`name`), UNIQUE INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` (`email`), UNIQUE INDEX `IDX_059e69c318702e93998f26d152` (`displayName`), PRIMARY KEY (`id`)) ENGINE=InnoDB", undefined); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query("DROP INDEX `IDX_059e69c318702e93998f26d152` ON `user`", undefined); | ||
await queryRunner.query("DROP INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` ON `user`", undefined); | ||
await queryRunner.query("DROP INDEX `IDX_065d4d8f3b5adb4a08841eae3c` ON `user`", undefined); | ||
await queryRunner.query("DROP TABLE `user`", undefined); | ||
} | ||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
'CREATE TABLE `user` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, `displayName` varchar(255) NOT NULL, `hashedPassword` varchar(255) NULL, UNIQUE INDEX `IDX_065d4d8f3b5adb4a08841eae3c` (`name`), UNIQUE INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` (`email`), UNIQUE INDEX `IDX_059e69c318702e93998f26d152` (`displayName`), PRIMARY KEY (`id`)) ENGINE=InnoDB', | ||
undefined | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
'DROP INDEX `IDX_059e69c318702e93998f26d152` ON `user`', | ||
undefined | ||
) | ||
await queryRunner.query( | ||
'DROP INDEX `IDX_e12875dfb3b1d92d7d7c5377e2` ON `user`', | ||
undefined | ||
) | ||
await queryRunner.query( | ||
'DROP INDEX `IDX_065d4d8f3b5adb4a08841eae3c` ON `user`', | ||
undefined | ||
) | ||
await queryRunner.query('DROP TABLE `user`', undefined) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { MigrationInterface, QueryRunner } from 'typeorm' | ||
|
||
export class CreateResource1580309144281 implements MigrationInterface { | ||
name = 'CreateResource1580309144281' | ||
|
||
public async up(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query( | ||
'CREATE TABLE `resource` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `description` text NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB', | ||
undefined | ||
) | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<any> { | ||
await queryRunner.query('DROP TABLE `resource`', undefined) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm' | ||
import { Field, ID, ObjectType } from 'type-graphql' | ||
|
||
@ObjectType() | ||
@Entity() | ||
class Resource { | ||
@Field(type => ID) | ||
@PrimaryGeneratedColumn('uuid') | ||
id!: string | ||
|
||
@Field() | ||
@Column() | ||
name!: string | ||
|
||
@Field({ nullable: true }) | ||
@Column({ type: 'text', nullable: true }) | ||
description?: string | ||
} | ||
export default Resource |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { AppError } from 'lib/error' | ||
export class AuthenticationError extends AppError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
export class AppError extends Error { | ||
constructor(message?: string) { | ||
super(message) | ||
Object.setPrototypeOf(this, new.target.prototype) | ||
Object.defineProperty(this, 'name', { | ||
get: () => this.constructor.name | ||
}) | ||
if (Error.captureStackTrace) { | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { AppError } from './AppError' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,2 @@ | ||
export class CannotSignInError extends Error { | ||
constructor(message: string) { | ||
super() | ||
Object.defineProperty(this, 'name', { | ||
get: () => this.constructor.name | ||
}) | ||
Object.defineProperty(this, 'message', { | ||
get: () => { | ||
return `${message}` | ||
} | ||
}) | ||
Error.captureStackTrace(this, this.constructor) | ||
} | ||
} | ||
import { AppError } from 'lib/error' | ||
export class CannotSignInError extends AppError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import { AppError } from 'lib/error' | ||
export class ResourceNotFoundError extends AppError {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Arg, Authorized, Mutation, Query, Resolver } from 'type-graphql' | ||
import Resource from 'entities/Resource' | ||
import { ResourceNotFoundError } from './ResourceNotFoundError' | ||
import { getRepository } from 'typeorm' | ||
|
||
@Resolver() | ||
class ResourceResolver { | ||
repository = getRepository(Resource) | ||
|
||
@Authorized() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@Mutation(returns => Resource) | ||
async createResource( | ||
@Arg('name') name: string, | ||
@Arg('description', { nullable: true }) description?: string | ||
): Promise<Resource> { | ||
const resource = new Resource() | ||
resource.name = name | ||
resource.description = description | ||
await this.repository.save(resource) | ||
return resource | ||
} | ||
|
||
@Authorized() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@Query(returns => Resource) | ||
async resource(@Arg('id') id: string): Promise<Resource> { | ||
const resource = await this.repository | ||
.findOneOrFail({ where: { id } }) | ||
.catch(reason => { | ||
const e = new ResourceNotFoundError(reason) | ||
console.log(e) | ||
throw e | ||
}) | ||
return resource | ||
} | ||
|
||
@Authorized() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@Query(returns => [Resource]) | ||
async resources(): Promise<Resource[]> { | ||
const resources = await this.repository.find() | ||
return resources | ||
} | ||
|
||
@Authorized() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@Mutation(returns => Resource) | ||
async updateResource( | ||
@Arg('id') id: string, | ||
@Arg('name', { nullable: true }) name?: string, | ||
@Arg('description', { nullable: true }) description?: string, | ||
@Arg('deleteDescription', { defaultValue: false }) | ||
deleteDescription?: boolean | ||
): Promise<Resource> { | ||
const resource = await this.repository.findOneOrFail({ where: { id } }) | ||
resource.name = name || resource.name | ||
resource.description = description || resource.description | ||
if (deleteDescription) { | ||
resource.description = undefined | ||
} | ||
await this.repository.save(resource) | ||
return resource | ||
} | ||
|
||
@Authorized() | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
@Mutation(returns => Boolean) | ||
async deleteResource(@Arg('id') id: string): Promise<boolean> { | ||
const resource = await this.repository.findOneOrFail({ where: { id } }) | ||
await this.repository.remove(resource) | ||
return true | ||
} | ||
} | ||
export default ResourceResolver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { default as ResourceResolver } from './ResourceResolver' | ||
export { default as UserResolver } from './UserResolver' | ||
export { default as UserSessionResolver } from './UserSessionResolver' | ||
export { default as WorldResolver } from './WorldResolver' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.