Skip to content

Commit

Permalink
fix: improve error message (fix #96)
Browse files Browse the repository at this point in the history
  • Loading branch information
brillout committed Jan 1, 2024
1 parent 77b174c commit d42f963
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion docs/pages/root.page.server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import { config } from 'telefunc'
config.root = __dirname
```

> If you have a monorepo structure, then set `config.root` to the root directory of your client-side (i.e. the root of Vite/Vike/Next.js/Nuxt/...), otherwise Telefunc will not be able to match your `.telefunc.js` files/imports between the server-side and the client-side.
> If you have a monorepo structure, then set `config.root` to the root directory of your client-side (i.e. the root of Vite/Vike/Next.js/Nuxt/...). Don't set `config.root` to the monorepo root, nor to the root directory of your server. (The `config.root` setting enables Telefunc to match your `.telefunc.js` files/imports between the server-side and the client-side.)
It's only needed if you use <Link href="/telefuncFiles">`config.telefuncFiles`</Link>.
18 changes: 17 additions & 1 deletion telefunc/node/server/runTelefunc/findTelefunction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { assert, assertUsage, errorPrefix as projectErrorPrefix } from '../../ut
import type { TelefuncFiles, Telefunction } from '../types'
import { assertNamingConvention } from './assertNamingConvention'
import { assertTelefunction } from './assertTelefunction'
import { getServerConfig } from '../serverConfig'
import pc from '@brillout/picocolors'

function findTelefunction(runContext: {
telefuncFilePath: string
Expand Down Expand Up @@ -53,7 +55,21 @@ function findTelefunction(runContext: {
function getNotFoundErrMsg() {
let errMsg = `Telefunction ${runContext.telefunctionName}() (${runContext.telefuncFilePath}) not found: `
if (!telefuncFile) {
errMsg += `the file ${runContext.telefuncFilePath} doesn't exist. Found \`.telefunc.js\` files:`
let extraMsg: string | null = null
const serverConfig = getServerConfig()
if (serverConfig.telefuncFiles) {
assert(serverConfig.telefuncFiles.length > 0)
assert(serverConfig.root)
extraMsg = `Did you set ${pc.cyan('config.root')} to the *client-side* root (see https://telefunc.com/root)?`
}
errMsg += [
`the file ${runContext.telefuncFilePath} doesn't exist.`,
// Hint about config.root
extraMsg,
'Found `.telefunc.js` files:'
]
.filter(Boolean)
.join(' ')
assert(!runContext.telefuncFilesAll.includes(runContext.telefuncFilePath))
errMsg += [runContext.telefuncFilePath, ...runContext.telefuncFilesAll]
.sort()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function transformTelefuncFileClientSideSync(
assertPosixPath(telefuncFilePath)
assertUsage(
!telefuncFilePath.startsWith('../'),
`The telefunc file ${telefuncFilePath} needs to live inside the root directory ${appRootDir} (wich is the root directory of Vite/Vike/Next.js/Nuxt/...)`
`The telefunc file ${telefuncFilePath} needs to live inside ${appRootDir} (the client-side root directory, i.e. the root directory of Vite/Vike/Next.js/Nuxt/...)`
)
assert(!telefuncFilePath.startsWith('/') && !telefuncFilePath.startsWith('.'))
telefuncFilePath = `/${telefuncFilePath}`
Expand Down

0 comments on commit d42f963

Please sign in to comment.