1313 */
1414import { parseStringPromise } from 'xml2js' ;
1515import type { AuthStrategy } from '../auth/types.js' ;
16+ import { HTTPError } from '../errors/http-error.js' ;
1617import { getLogger } from '../logging/logger.js' ;
1718
1819/**
@@ -66,8 +67,11 @@ export class WebDavClient {
6667
6768 /**
6869 * Builds the full URL for a WebDAV path.
70+ *
71+ * @param path - Path relative to /webdav/Sites/
72+ * @returns Full URL
6973 */
70- private buildUrl ( path : string ) : string {
74+ buildUrl ( path : string ) : string {
7175 const cleanPath = path . startsWith ( '/' ) ? path . slice ( 1 ) : path ;
7276 return `${ this . baseUrl } /${ cleanPath } ` ;
7377 }
@@ -167,8 +171,7 @@ export class WebDavClient {
167171
168172 // 201 = created, 405 = already exists (acceptable)
169173 if ( ! response . ok && response . status !== 405 ) {
170- const text = await response . text ( ) ;
171- throw new Error ( `MKCOL failed: ${ response . status } ${ response . statusText } - ${ text } ` ) ;
174+ throw new HTTPError ( `MKCOL failed: ${ response . status } ${ response . statusText } ` , response , 'MKCOL' ) ;
172175 }
173176 }
174177
@@ -188,15 +191,10 @@ export class WebDavClient {
188191 headers [ 'Content-Type' ] = contentType ;
189192 }
190193
191- const response = await this . request ( path , {
192- method : 'PUT' ,
193- headers,
194- body : content ,
195- } ) ;
194+ const response = await this . request ( path , { method : 'PUT' , headers, body : content } ) ;
196195
197196 if ( ! response . ok ) {
198- const text = await response . text ( ) ;
199- throw new Error ( `PUT failed: ${ response . status } ${ response . statusText } - ${ text } ` ) ;
197+ throw new HTTPError ( `PUT failed: ${ response . status } ${ response . statusText } ` , response , 'PUT' ) ;
200198 }
201199 }
202200
@@ -213,7 +211,7 @@ export class WebDavClient {
213211 const response = await this . request ( path , { method : 'GET' } ) ;
214212
215213 if ( ! response . ok ) {
216- throw new Error ( `GET failed: ${ response . status } ${ response . statusText } ` ) ;
214+ throw new HTTPError ( `GET failed: ${ response . status } ${ response . statusText } ` , response , 'GET' ) ;
217215 }
218216
219217 return response . arrayBuffer ( ) ;
@@ -223,17 +221,16 @@ export class WebDavClient {
223221 * Deletes a file or directory.
224222 *
225223 * @param path - Path to delete
224+ * @throws Error if the path doesn't exist (404) or deletion fails
226225 *
227226 * @example
228227 * await client.delete('Cartridges/v1/old-cartridge');
229228 */
230229 async delete ( path : string ) : Promise < void > {
231230 const response = await this . request ( path , { method : 'DELETE' } ) ;
232231
233- // 404 is acceptable (already deleted)
234- if ( ! response . ok && response . status !== 404 ) {
235- const text = await response . text ( ) ;
236- throw new Error ( `DELETE failed: ${ response . status } ${ response . statusText } - ${ text } ` ) ;
232+ if ( ! response . ok ) {
233+ throw new HTTPError ( `DELETE failed: ${ response . status } ${ response . statusText } ` , response , 'DELETE' ) ;
237234 }
238235 }
239236
@@ -270,8 +267,7 @@ export class WebDavClient {
270267 } ) ;
271268
272269 if ( ! response . ok ) {
273- const text = await response . text ( ) ;
274- throw new Error ( `PROPFIND failed: ${ response . status } ${ response . statusText } - ${ text } ` ) ;
270+ throw new HTTPError ( `PROPFIND failed: ${ response . status } ${ response . statusText } ` , response , 'PROPFIND' ) ;
275271 }
276272
277273 const xml = await response . text ( ) ;
0 commit comments