-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export program error codes as constants on experimental renderer (#215)
- Loading branch information
1 parent
010f4d4
commit 092bc77
Showing
17 changed files
with
824 additions
and
1,456 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@metaplex-foundation/kinobi": minor | ||
--- | ||
|
||
Export program error codes as constants on experimental renderer |
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,22 +1,3 @@ | ||
{% import "templates/macros.njk" as macros %} | ||
|
||
export const {{ programAddressConstant }} = '{{ program.publicKey }}' as Address<'{{ program.publicKey }}'>; | ||
|
||
{{ macros.docblock(program.docs) }} | ||
export type {{ programType }} = Program<'{{ program.publicKey }}'> | ||
{%- if program.errors.length > 0 %} | ||
& ProgramWithErrors<{{ programErrorCode }}, {{ programErrorClass }}> | ||
{% endif %} | ||
; | ||
|
||
export function {{ programCreateFunction }}(): {{ programType }}{ | ||
return { | ||
name: '{{ program.name | camelCase }}', | ||
address: {{ programAddressConstant }}, | ||
{% if program.errors.length > 0 %} | ||
getErrorFromCode(code: {{ programErrorCode }}, cause?: Error) { | ||
return {{ programGetErrorFromCodeFunction }}(code, cause); | ||
}, | ||
{% endif %} | ||
}; | ||
} |
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
41 changes: 15 additions & 26 deletions
41
src/renderers/js-experimental/fragments/programErrors.njk
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,39 +1,28 @@ | ||
{% import "templates/macros.njk" as macros %} | ||
|
||
export const enum {{ programErrorCodeEnum }} { | ||
{% for error in errors | sort(false, false, 'code') %} | ||
{{ macros.docblock(error.docs) }} | ||
{{ error.name | snakeCase | upper }} = 0x{{ error.code.toString(16) }}, // {{ error.code }} | ||
{% endfor %} | ||
} | ||
|
||
export class {{ programErrorClass }} extends Error { | ||
override readonly name = '{{ programErrorClass }}'; | ||
|
||
readonly code: {{ programErrorCodeEnum }}; | ||
{% for error in errors | sort(false, false, 'code') %} | ||
{{ macros.docblock(error.docs) }} | ||
export const {{ getProgramErrorConstant(error.name) }} = 0x{{ error.code.toString(16) }}, // {{ error.code }} | ||
{% endfor %} | ||
|
||
readonly cause: Error | undefined; | ||
|
||
constructor(code: {{ programErrorCodeEnum }}, name: string, message: string, cause?: Error) { | ||
super(`${name} (${code}): ${message}`); | ||
this.code = code; | ||
this.cause = cause; | ||
} | ||
} | ||
export type {{ programErrorUnion }} = | ||
{% for error in errors | sort(false, false, 'name') %} | ||
| typeof {{ getProgramErrorConstant(error.name) }} | ||
{% endfor %}; | ||
|
||
let {{ programErrorCodeMap }}: Record<{{ programErrorCodeEnum }}, [string, string]> | undefined; | ||
let {{ programErrorMessagesMap }}: Record<{{ programErrorUnion }}, string> | undefined; | ||
if (__DEV__) { | ||
{{ programErrorCodeMap }} = { | ||
{% for error in errors | sort(false, false, 'code') %} | ||
[{{ programErrorCodeEnum }}.{{ error.name | snakeCase | upper }}]: ['{{ error.name | pascalCase }}', `{{ error.message }}`], | ||
{{ programErrorMessagesMap }} = { | ||
{% for error in errors | sort(false, false, 'name') %} | ||
[{{ getProgramErrorConstant(error.name) }}]: `{{ error.message }}`, | ||
{% endfor %} | ||
}; | ||
} | ||
|
||
export function {{ programGetErrorFromCodeFunction }}(code: {{ programErrorCodeEnum }}, cause?: Error): {{ programErrorClass }} { | ||
export function {{ programGetErrorMessageFunction }}(code: {{ programErrorUnion }}): string { | ||
if (__DEV__) { | ||
return new {{ programErrorClass }}(code, ...({{ programErrorCodeMap }} as Record<{{ programErrorCodeEnum }}, [string, string]>)[code], cause); | ||
return ({{ programErrorMessagesMap }} as Record<{{ programErrorUnion }}, string>)[code]; | ||
} | ||
|
||
return new {{ programErrorClass }}(code, 'Unknown', 'Error message not available in production bundles. Compile with __DEV__ set to true to see more information.', cause); | ||
return 'Error message not available in production bundles. Compile with `__DEV__` set to true to see more information.'; | ||
} |
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
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.