Skip to content
Snippets Groups Projects
Commit b9d4e722 authored by Nawfel Senoussi's avatar Nawfel Senoussi
Browse files

improve error handling

parent 84645c1e
No related branches found
No related tags found
No related merge requests found
...@@ -60,13 +60,12 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { ...@@ -60,13 +60,12 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> {
} }
}) })
console.log("Création de l'association en cours...")
try { try {
const op = await createAssociation(association, tezos) const op = await createAssociation(association, tezos)
console.log(op.hash) console.log(op.hash)
console.log(chalk.green('\nVotre association a été créée !!\n')) console.log(chalk.bgGreenBright('\nVotre association a été créée !!\n'))
} catch { } catch (error) {
console.log(chalk.bgRedBright("\nErreur lors de la création de l'association\n")) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -103,9 +102,9 @@ async function handleCreateToken (tezos: TezosToolkit): Promise<void> { ...@@ -103,9 +102,9 @@ async function handleCreateToken (tezos: TezosToolkit): Promise<void> {
try { try {
const op = await createFAToken(nbTokenFungible, tezos) const op = await createFAToken(nbTokenFungible, tezos)
console.log(op.hash) console.log(op.hash)
console.log(chalk.green('\nVotre token a été créé !!\n')) console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n'))
} catch { } catch (error) {
console.log(chalk.bgRedBright('\nErreur lors de la création du token\n')) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -118,8 +117,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> { ...@@ -118,8 +117,8 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> {
try { try {
const balance = await getBalance(tezos) const balance = await getBalance(tezos)
console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`)
} catch { } catch (error) {
console.log(chalk.bgRedBright('\nErreur lors de la récupération de votre portefeuille\n')) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -170,12 +169,11 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { ...@@ -170,12 +169,11 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> {
}) })
try { try {
console.log('Création de la proposition en cours...')
const op = await createProposal(proposal, tezos) const op = await createProposal(proposal, tezos)
console.log(op.hash) console.log(op.hash)
console.log(chalk.green('\nVous avez soumis une proposition !!\n')) console.log(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n'))
} catch { } catch (error) {
console.log(chalk.bgRedBright('\nErreur lors de la création de la proposition\n')) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -209,14 +207,11 @@ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { ...@@ -209,14 +207,11 @@ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> {
}) })
try { try {
console.log("Inscription à l'association en cours...")
const op = await joinAssociation(associationName, tezos) const op = await joinAssociation(associationName, tezos)
console.log(op.hash) console.log(op.hash)
console.log(chalk.green("\nVous avez rejoint l'association !!\n")) console.log(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n"))
} catch { } catch (error) {
// ERREUR -> PAS ASSEZ DE TOKEN console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
// ERREUR -> ASSOCIATION NON EXISTANTE
console.log(chalk.bgRedBright("\nErreur lors de l'inscription à l'association\n"))
} }
} }
...@@ -227,12 +222,11 @@ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { ...@@ -227,12 +222,11 @@ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> {
*/ */
async function handleGetAssociations (tezos: TezosToolkit): Promise<void> { async function handleGetAssociations (tezos: TezosToolkit): Promise<void> {
try { try {
console.log('Récupération des associations en cours...')
const op = await getAssociations(tezos) const op = await getAssociations(tezos)
console.log(op.hash) console.log(op.hash)
// LISTER LES ASSOCIATIONS // LISTER LES ASSOCIATIONS
} catch { } catch (error) {
console.log(chalk.bgRedBright('\nErreur lors de la récupération des associations\n')) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -266,12 +260,11 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> ...@@ -266,12 +260,11 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void>
}) })
try { try {
console.log("Récupération des détails de l'association en cours...")
const op = await getAssociationDetails(associationName, tezos) const op = await getAssociationDetails(associationName, tezos)
console.log(op.hash) console.log(op.hash)
// RETURN ASSOCIATIONS // RETURN ASSOCIATIONS
} catch { } catch (error) {
console.log(chalk.bgRedBright("\nErreur lors de la récupération des détails de l'association\n")) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
...@@ -306,12 +299,11 @@ async function handleBurnToken (tezos: TezosToolkit): Promise<void> { ...@@ -306,12 +299,11 @@ async function handleBurnToken (tezos: TezosToolkit): Promise<void> {
}) })
try { try {
console.log('Brulure des tokens en cours...')
const op = await burnToken(nbTokenToBurn, tezos) const op = await burnToken(nbTokenToBurn, tezos)
console.log(op.hash) console.log(op.hash)
console.log(chalk.green('\nVous avez bruler vos tokens !!\n')) console.log(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n'))
} catch { } catch (error) {
console.log(chalk.bgRedBright('\nErreur lors de la brulure de vos tokens\n')) console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`))
} }
} }
......
...@@ -2,7 +2,7 @@ import { type Operation, type TezosToolkit } from '@taquito/taquito' ...@@ -2,7 +2,7 @@ import { type Operation, type TezosToolkit } from '@taquito/taquito'
import { type Association } from '../types/Association' import { type Association } from '../types/Association'
// NEED UPDATE ADDRESS !! (SMART CONTRACT 1: Registre des associations) // NEED UPDATE ADDRESS !! (SMART CONTRACT 1: Registre des associations)
const address = 'KT1PCg4GbtF5LQQhHmY4t1qNYVspnitKGHgm' const address = 'KT1NMZvpAEQmezU8kHKzgi9PjysoH4VTcB3P'
// NEED UPDATE ENTRYPOINT !! // NEED UPDATE ENTRYPOINT !!
async function createAssociation (association: Association, tezos: TezosToolkit): Promise<Operation> { async function createAssociation (association: Association, tezos: TezosToolkit): Promise<Operation> {
......
...@@ -76,13 +76,20 @@ describe('handlers', () => { ...@@ -76,13 +76,20 @@ describe('handlers', () => {
{ name: 'Association Name', description: 'Association Description' }, { name: 'Association Name', description: 'Association Description' },
mockedTezosToolkit mockedTezosToolkit
) )
expect(consoleSpy).toBeCalledWith(chalk.green('\nVotre association a été créée !!\n')) expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre association a été créée !!\n'))
}) })
}) })
describe('when createAssociation is called with error', () => { describe('when createAssociation is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
createAssociationSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createAssociationSpy.mockRejectedValueOnce(error)
const name = 'Association Name' const name = 'Association Name'
const description = 'Association Description' const description = 'Association Description'
...@@ -97,7 +104,7 @@ describe('handlers', () => { ...@@ -97,7 +104,7 @@ describe('handlers', () => {
{ name: 'Association Name', description: 'Association Description' }, { name: 'Association Name', description: 'Association Description' },
mockedTezosToolkit mockedTezosToolkit
) )
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright("\nErreur lors de la création de l'association\n")) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -113,13 +120,20 @@ describe('handlers', () => { ...@@ -113,13 +120,20 @@ describe('handlers', () => {
await handleCreateToken(mockedTezosToolkit) await handleCreateToken(mockedTezosToolkit)
expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.green('\nVotre token a été créé !!\n')) expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n'))
}) })
}) })
describe('when createFAToken is called with error', () => { describe('when createFAToken is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
createAssociationSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createAssociationSpy.mockRejectedValueOnce(error)
const nbTokenFungible = 5 const nbTokenFungible = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
...@@ -128,7 +142,7 @@ describe('handlers', () => { ...@@ -128,7 +142,7 @@ describe('handlers', () => {
await handleCreateToken(mockedTezosToolkit) await handleCreateToken(mockedTezosToolkit)
expect(createAssociationSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) expect(createAssociationSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.green('\nErreur lors de la création du token\n')) expect(consoleSpy).toBeCalledWith(chalk.red('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -144,14 +158,21 @@ describe('handlers', () => { ...@@ -144,14 +158,21 @@ describe('handlers', () => {
describe('when getBalance is called with error', () => { describe('when getBalance is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
getBalanceSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getBalanceSpy.mockRejectedValueOnce(error)
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await handleGetBalance(mockedTezosToolkit) await handleGetBalance(mockedTezosToolkit)
expect(getBalanceSpy).toBeCalled() expect(getBalanceSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la récupération de votre portefeuille\n')) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -168,13 +189,20 @@ describe('handlers', () => { ...@@ -168,13 +189,20 @@ describe('handlers', () => {
await handleCreateProposal(mockedTezosToolkit) await handleCreateProposal(mockedTezosToolkit)
expect(createProposalSpy).toBeCalledWith({ title: 'Proposal Title', description: 'Proposal Description' }, mockedTezosToolkit) expect(createProposalSpy).toBeCalledWith({ title: 'Proposal Title', description: 'Proposal Description' }, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.green('\nVous avez soumis une proposition !!\n')) expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n'))
}) })
}) })
describe('when createProposal is called with error', () => { describe('when createProposal is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
createProposalSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
createProposalSpy.mockRejectedValueOnce(error)
const title = 'Proposal Title' const title = 'Proposal Title'
const description = 'Proposal Description' const description = 'Proposal Description'
...@@ -184,7 +212,7 @@ describe('handlers', () => { ...@@ -184,7 +212,7 @@ describe('handlers', () => {
await handleCreateProposal(mockedTezosToolkit) await handleCreateProposal(mockedTezosToolkit)
expect(createProposalSpy).toBeCalled() expect(createProposalSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la création de la proposition\n')) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -200,13 +228,20 @@ describe('handlers', () => { ...@@ -200,13 +228,20 @@ describe('handlers', () => {
await handleJoinAssociation(mockedTezosToolkit) await handleJoinAssociation(mockedTezosToolkit)
expect(joinAssociationSpy).toBeCalledWith('Association Name', mockedTezosToolkit) expect(joinAssociationSpy).toBeCalledWith('Association Name', mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.green("\nVous avez rejoint l'association !!\n")) expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n"))
}) })
}) })
describe('when joinAssociation is called with error', () => { describe('when joinAssociation is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
joinAssociationSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
joinAssociationSpy.mockRejectedValueOnce(error)
const name = 'Association Name' const name = 'Association Name'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
...@@ -215,7 +250,7 @@ describe('handlers', () => { ...@@ -215,7 +250,7 @@ describe('handlers', () => {
await handleJoinAssociation(mockedTezosToolkit) await handleJoinAssociation(mockedTezosToolkit)
expect(joinAssociationSpy).toBeCalled() expect(joinAssociationSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright("\nErreur lors de l'inscription à l'association\n")) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -231,14 +266,21 @@ describe('handlers', () => { ...@@ -231,14 +266,21 @@ describe('handlers', () => {
describe('when getAssociations is called with error', () => { describe('when getAssociations is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
getAssociationsSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getAssociationsSpy.mockRejectedValueOnce(error)
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
await handleGetAssociations(mockedTezosToolkit) await handleGetAssociations(mockedTezosToolkit)
expect(getAssociationsSpy).toBeCalled() expect(getAssociationsSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la récupération des associations\n')) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -257,7 +299,14 @@ describe('handlers', () => { ...@@ -257,7 +299,14 @@ describe('handlers', () => {
describe('when getAssociationDetails is called with error', () => { describe('when getAssociationDetails is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
getAssociationDetailsSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
getAssociationDetailsSpy.mockRejectedValueOnce(error)
const associationName = 'Association Name' const associationName = 'Association Name'
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
...@@ -266,7 +315,7 @@ describe('handlers', () => { ...@@ -266,7 +315,7 @@ describe('handlers', () => {
await handleGetAssociationDetails(mockedTezosToolkit) await handleGetAssociationDetails(mockedTezosToolkit)
expect(getAssociationDetailsSpy).toBeCalled() expect(getAssociationDetailsSpy).toBeCalled()
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright("\nErreur lors de la récupération des détails de l'association\n")) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
...@@ -282,13 +331,20 @@ describe('handlers', () => { ...@@ -282,13 +331,20 @@ describe('handlers', () => {
await handleBurnToken(mockedTezosToolkit) await handleBurnToken(mockedTezosToolkit)
expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.green('\nVous avez bruler vos tokens !!\n')) expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n'))
}) })
}) })
describe('when burnToken is called with error', () => { describe('when burnToken is called with error', () => {
it('should log error message', async () => { it('should log error message', async () => {
burnTokenSpy.mockRejectedValueOnce({}) const error = {
lastError: {
with: {
string: 'Custom Error'
}
}
}
burnTokenSpy.mockRejectedValueOnce(error)
const nbTokenToBurn = 5 const nbTokenToBurn = 5
const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {})
...@@ -297,7 +353,7 @@ describe('handlers', () => { ...@@ -297,7 +353,7 @@ describe('handlers', () => {
await handleBurnToken(mockedTezosToolkit) await handleBurnToken(mockedTezosToolkit)
expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit)
expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la brulure de vos tokens\n')) expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n'))
}) })
}) })
}) })
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment